liveBook · Manning

Async/await

As explained in Secrets of the JavaScript Ninja, Third Edition

JavaScript syntax for expressing asynchronous control flow using async functions and await expressions.

A request handler may need to read a file or query a database before it can respond. Waiting for that result must not block JavaScript from handling other events. `async` and `await` let promise-based operations read like sequential code without giving up non-blocking behavior. section 3.1 section 8.2.1

`await value` converts `value` to a promise, as `Promise.resolve(value)` would, and pauses the surrounding execution until that promise settles. While it is pending, the event loop can process other events. A fulfilled promise resumes execution with its value; a rejected promise throws its reason at the `await` expression. Resumption is queued as a microtask, much like a `then` callback. section 8.2.2

async function indexHandler(req, res) {
  const html = await fsPromises.readFile("index.html", "utf8");
  res.send(html);
}

The request handler awaits a promise-based file read, then sends the resulting HTML. The code is written in order, but the pending read does not block other events. section 3.1

This helps when later steps need earlier results. Nested callbacks keep that state in scope but can become hard to read; separating them requires passing state around explicitly. With `await`, authentication, a database query, and rendering can remain in one readable flow. Ordinary `try`/`catch` can handle a rejected awaited operation. section 8.2.1 section 8.2.2

Keep these boundaries in mind:

  • `await` is allowed inside an `async` function or at the top level of an ECMAScript module—not anywhere in JavaScript code. section 8.2.2
  • An `async` function always returns a promise. Its returned value or thrown error determines how that promise settles. section 8.4
  • Sequential-looking syntax does not create another JavaScript thread. It replaces common promise callback patterns while leaving other events free to run. section 8.1 section 8.2.2

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Explains promise conversion, event-loop yielding, microtask resumption, fulfillment, rejection, and where await is allowed.
Examples
Uses a timer promise, a Node.js file-read handler, and an authentication-and-database request flow.
What it assumes
Builds on JavaScript callbacks and promises, introducing promise behavior before explaining await’s mechanics.
What you can do after
Apply it via Node.js fsPromises.readFile to write a non-blocking request handler with sequential-looking code.
How it connects
Connects promises, callbacks, the event loop, microtasks, error handling, and ECMAScript modules.

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 Secrets of the JavaScript Ninja, Third Edition. The section links point to the parts of the book it draws on.