await does block — just not where you think

2026/03/21
javascriptasync

Most of us carry a deep-rooted idea that async/await is non-blocking. But look at this:

async function getData() {
  const data = await fetch('/api');
  console.log(data); // this line waits
}

console.log(data) cannot run until fetch finishes. That is waiting. That is blocking. So why does everyone keep saying it's not?

Because "non-blocking" refers to the thread, not the next line.

The confusion

The word "blocking" gets used without saying what is being blocked. Inside the function, await absolutely blocks the lines below it. The function pauses and the remaining code waits.

But step outside:

getData();
console.log('This runs right away');

console.log fires immediately. It does not wait for fetch. The caller moved on. The main thread kept going.

So await blocks the function it lives in. It does not block whoever called that function — unless the caller also uses await.

Nested await

Three layers, each with await:

const delay = (ms) => new Promise((r) => setTimeout(r, ms));
 
async function fetchData() {
  console.log('3. fetchData: before await');
  await delay(500);
  console.log('3. fetchData: after await');
}
 
async function process() {
  console.log('2. process: before await');
  await fetchData();
  console.log('2. process: after await');
}
 
async function main() {
  console.log('1. main: before await');
  await process();
  console.log('1. main: after await');
}
 
main();
console.log('0. main thread: immediate');

Output (indented by call depth here; the real output has no indentation):

1. main: before await
   2. process: before await
      3. fetchData: before await
0. main thread: immediate             ← all three paused at await, this line prints first
      3. fetchData: after await       ← 500ms later, innermost resumes first
   2. process: after await
1. main: after await                  ← outermost resumes last

0. main thread: immediate is the fourth line because main calls process, process calls fetchData, and fetchData reaches await delay(500). Only there does execution suspend and return control to the caller. Until it actually awaits something, an async function runs in the same order as a regular function: synchronously, top to bottom.

After 500ms, each of the three functions resumes from the line after its await, innermost first, in the same order a call stack returns. main() was called without await, so 0. main thread: immediate does not wait for those 500ms.

Same order, but an async function still differs from a regular function in two ways. With async function g() { return 42 }, calling g() gives you Promise { 42 }, not 42. Exceptions differ too:

async function f() { throw new Error('boom'); }
try { f(); } catch (e) { console.log('caught'); } // never prints

f throws as soon as it is entered, before reaching any await. By the paragraph above it is running synchronously, so you would expect the caller's try/catch to catch it. It does not, because an exception thrown inside an async function becomes a rejected promise instead of propagating to the caller. To catch it, write await f() or f().catch(...). Both examples were run on Node 22.22.3.

What if the top level also awaits?

Change main() to await main():

await main();
console.log('0. main thread: immediate');
1. main: before await
   2. process: before await
      3. fetchData: before await
      3. fetchData: after await       ← 500ms later
   2. process: after await
1. main: after await
0. main thread: immediate             ← the top level awaits now, so this line waits too

Top-level await only works in an ES module (ESM). A .mjs file is ESM. A plain .js file is switched to ESM automatically from Node 22.7 when it contains this syntax. A .cjs file is always treated as CommonJS and throws SyntaxError: await is only valid in async functions and the top level bodies of modules.

So which is it — blocking or not?

Inside a function, every line after await waits. If the caller also writes await, it waits too. With await all the way to the top level, even the last console.log waits.

What non-blocking really means: during those 500ms, can the same thread handle other requests? Node runs JavaScript on one thread, and all requests take turns running on it. This server has two routes; /a waits 500ms and /b answers right away:

import http from 'node:http';
const wait = (ms) => new Promise((r) => setTimeout(r, ms));
 
http.createServer(async (req, res) => {
  if (req.url === '/a') {
    await wait(500);                                  // version 1: await
    // const end = Date.now() + 500;
    // while (Date.now() < end) {}                    // version 2: synchronous busy loop
    return res.end('a');
  }
  res.end('b');
}).listen(3000);

Request /a, then /b 50ms later, once with each version (Node 22.22.3). All times are counted from the /a request:

version 1 await:      /a answered at 510ms, /b at 60ms
version 2 busy loop:  /a answered at 512ms, /b at 513ms

In version 1, /a suspends at await and the thread is free to handle /b, which is sent at 50ms and answered at 60ms, 10ms later. In version 2 the thread spends the whole 500ms in that while, so /b queues behind /a and answers only after /a does. fs.readFileSync on a large file behaves like version 2: the thread is held until the file is read.

At 500ms the promise from wait(500) settles, and /a resumes on the line after await and sends its response. If the thread is busy with another request at that moment, and that request's handler has a while like version 2, /a has to wait until it finishes.

So /a itself did wait 500ms, but /b did not wait just because /a was waiting, and that is non-blocking.