Learning on Web Dev Open is free for all.

Systems Thinking > One thread, several queuesA promise is a value, not a callback
Phase 03One thread, several queues141 of 434

A promise is a value, not a callback

The shift that makes async composable: the pending result is a thing you can hold, name, store in an array and pass around before it exists.

Concept13 minAI pair

Callbacks invert control: you hand your code to someone else and hope they call it once. A promise gives you back a value representing the eventual result, and values compose. You can put promises in an array, pass one to two different consumers, kick one off now and await it three functions later, or return one from a function whose caller decides when to care.

async/await is syntax over that value. An async function returns a promise the instant it hits its first await, and the rest of the body is scheduled as a continuation on the microtask queue. That is the entire mechanism, and it explains the two behaviours people find surprising: an async function with no await still returns a promise, and awaiting a non-promise still defers to the microtask queue.

The habit worth building is separating starting from awaiting. const a = fetchA(); const b = fetchB(); const [x, y] = [await a, await b]; runs both requests concurrently, where awaiting each at its call site runs them one after the other. Same-looking code, twice the latency. This distinction is the difference between understanding promises and reciting them.

You should now be able to

  • Explain what async/await desugars to
  • Store a pending operation and await it somewhere else
Ask the community

Loading…