Skip to content

Main Functions

Core application functions and entry points.

main()

The primary application function that demonstrates the template's capabilities.

Signature

typescript
export async function main(): Promise<void>

Description

Main application function that showcases:

  • Async/await patterns
  • Utility function usage
  • Console output with timestamps
  • Error handling via Promise rejection

Usage Example

typescript
import { main } from './index.js';

// Run the main application
await main();

// With error handling
try {
  await main();
  console.log('Application completed successfully');
} catch (error) {
  console.error('Application failed:', error);
}

Implementation Details

The function demonstrates:

  1. Timestamp logging using formatTimestamp()
  2. Async delays using delay()
  3. Modern ES modules import/export
  4. TSDoc documentation patterns

Error Handling

The function uses Promise-based error handling:

  • Returns Promise<void> on success
  • Rejects with error on failure
  • Errors bubble up to caller

Performance

  • Fast execution - typically completes in ~100ms
  • Minimal memory usage - no persistent state
  • Clean async patterns - proper Promise handling

Utility Integration

The main function integrates with utility functions:

Testing

See the test suite in __tests__/index.test.ts for comprehensive examples:

  • Normal execution testing
  • Error condition testing
  • Performance verification
  • Output validation

Best Practices Demonstrated

Async/Await Usage

typescript
// ✅ Good - proper async/await
export async function main(): Promise<void> {
  await delay(100);
  console.log('Completed');
}

// ❌ Bad - missing await
export async function main(): Promise<void> {
  delay(100); // Promise not awaited
  console.log('Completed'); // Runs immediately
}

Error Propagation

typescript
// ✅ Good - let errors bubble up
export async function main(): Promise<void> {
  await riskyOperation(); // Errors propagate naturally
}

// ❌ Bad - swallowing errors
export async function main(): Promise<void> {
  try {
    await riskyOperation();
  } catch {
    // Error silently ignored
  }
}

Type Safety

typescript
// ✅ Good - explicit return type
export async function main(): Promise<void> {
  // Implementation
}

// ❌ Bad - implicit return type
export async function main() {
  // TypeScript infers return type
}

Released under the MIT License.