Syntax over callbacks
The keywords async and await look sequential but compile into a state machine built around futures. Each await point becomes a place where the function can pause and later resume.
What the compiler builds
- An async function returns a future immediately, before its body runs to completion.
- Every await marks a suspension point where local state is captured.
- When the awaited future settles, the machine resumes at the saved point with the result.
So this readable code:
- step one runs
- await a network call
- step two runs with the result
becomes a switch over states zero, one, and two, where awaiting parks the function and hands control back to the caller.
Resumption
The runtime stores a continuation: the saved locals plus the next state. Settling the awaited future schedules that continuation to run, often back on the event loop.
Key idea
Async await is sugar for a future returning state machine that suspends at each await and resumes when that future settles.