Functions
Intermediate

Functions, Async Flows, and Typed Errors

Design function contracts for services, handlers, callbacks, and async workflows.

36 min
3 sections
functions
async
result
errors
1
2
3

01. Function Contracts

Section 1 of 3

Function types should describe what the caller must provide and what the function guarantees. In enterprise code, clear function boundaries are more valuable than clever inference because they communicate ownership between modules.

typescript
type AuditWriter = (event: {
  actorId: string;
  action: string;
  resourceId: string;
  metadata?: Record<string, string>;
}) => Promise<void>;

async function closeTicket(
  ticketId: string,
  actorId: string,
  writeAudit: AuditWriter
): Promise<void> {
  await writeAudit({
    actorId,
    action: "ticket.close",
    resourceId: ticketId,
  });
}
Back to Course