← Lessons

quiz vs the machine

Silver1100

Concurrency

Async Await Desugaring

How clean async code becomes a state machine.

4 min read · intro · beat Silver to climb

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.

Check yourself

Answer to earn rating on the learn ladder.

1. What does an async function return before its body finishes?

2. What does each await point become after desugaring?

3. How does a suspended async function resume?