liveBook · Manning

Async/await

As explained in JavaScript in Depth

A JavaScript operator that suspends an async function until a promise-like value settles, then yields its result or throws its rejection.

A suspended async function is not inert. At await, the runtime retains a promise, reaction record, closure, and suspended scope until the operation resolves. Under concurrent load, this normal retention can create old-generation garbage-collection pressure even without a leak. section 9.4.1

The continuation closure captures the function scope, including locals declared before await whether later code uses them or not. Pending operations therefore keep those objects alive across scavenges and may promote them to the old generation. section 9.4.1

async function handleRequest(req) {
  const user = await fetchUser(req.userId);  #A
  const posts = await fetchPosts(user.id);  #B
  const rendered = await renderTemplate(posts);  #C
  return rendered;
}

Trace three sequential await points and identify which promise, continuation, closure, and scope remain live at each suspension. section 9.4.1

How JavaScript in Depth covers it

How deep it goes
Descends into promises, reaction records, closures, captured scopes, scavenges, and promotion, but not await scheduling semantics beyond retention.
Examples
Uses sequential fetchUser, fetchPosts, and renderTemplate awaits to illustrate multiple suspended continuations.
What you can do after
Diagnose memory pressure by identifying promise-related objects retained while awaited operations remain pending.
How it connects
Connects async/await to promises, closures, lexical scope, heap allocation, and garbage collection.

Also explained in

Compare how 20 books explain async/await →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.