Sub topic 11
How Top-Level Await Changes Evaluation Order
This chapter assumes you know static import/export syntax, promises, and how to run ES modules in Node.js (.mjs files or "type": "module"). It also assumes you have read the previous chapter on top-level await. The goal here is to predict the order in which modules run once some of them contain await at the top level.
The Baseline: Depth-First, Post-Order
Without top-level await, the runtime loads the whole module graph first and then evaluates it. Evaluation is depth-first and post-order: each module's dependencies run before the module itself, in the order the import statements appear. A module that several others import is evaluated only once.
Evaluation is fully synchronous. Once it starts, every module body runs to completion before the next one begins.
What Top-Level Await Adds
A module that uses top-level await becomes an async module. Three rules follow from that:
- The async module runs synchronously until its first
await, then yields. - Every module that imports it, directly or through other modules, waits until it finishes. Those importing modules count as async too.
- Sibling modules that do not depend on the paused module keep evaluating while it waits.
Rule 3 surprises people. Top-level await blocks the modules above it in the graph. It does not block the whole graph.
A Worked Example
Save these three files in one folder:
// a.mjs
console.log('a: start');
await new Promise((resolve) => setTimeout(resolve, 100));
console.log('a: end');
export const a = 'A';// b.mjs
console.log('b: runs');
export const b = 'B';// main.mjs
import { a } from './a.mjs';
import { b } from './b.mjs';
console.log('main:', a, b);Run node main.mjs. The output is:
a: start
b: runs
a: end
main: A BModule a starts first because it is imported first. When a reaches its await, it yields. The runtime moves on to b, which does not depend on a, so b runs right away. main depends on both, so it waits until a finishes.
Dependents Wait, Even Indirectly
Suppose b.mjs imports a.mjs instead. Then b cannot run until a finishes, and main waits for both. The output becomes a: start, a: end, b: runs, main: A B. A single slow top-level await can delay every module along the import chain above it.
Dynamic import() and Async Modules
The promise returned by import() resolves only after the target module and all of its async dependencies have finished evaluating. Inside the target module, top-level await behaves exactly as it does with a static import. The only difference is that the calling code chooses when to wait.
// later.mjs
const mod = await import('./a.mjs'); // resolves after 'a: end'
console.log(mod.a);Summary of Rules
| Situation | Effect on evaluation |
|---|---|
| No top-level await anywhere | Synchronous depth-first, post-order |
| Module awaits at top level | Module pauses at the await, then resumes later |
| Module imports an async module | Waits until that module finishes |
| Sibling with no dependency on the async module | Runs while the async module is paused |
| `import()` of an async module | Promise resolves after evaluation finishes |
Exercise
main.mjs imports x.mjs, then y.mjs, then z.mjs, in that order. x.mjs logs "x1", awaits a 50 ms timer, then logs "x2". y.mjs imports x.mjs and logs "y". z.mjs has no imports and logs "z". main.mjs logs "main". What is the output?
Show answer
x1, z, x2, y, main. Module x pauses at its await. Module y depends on x, so it must wait. Module z does not depend on x, so it runs immediately. When x finishes, y runs, and then main runs.