top-level await
Using await directly in the body of an ES module, outside any async function, so the module waits for a promise before it finishes loading.
In more detail
Modules that import it wait too, which is handy for loading configuration before a server starts. It only works in ES modules because CommonJS loads files synchronously.
Related terms
- async and await — Language keywords for writing code that waits for slow work, such as a network call, without blocking a thread.
- ECMAScript modules — The standard JavaScript module system, written with import and export statements, used by browsers and modern tools.
- CommonJS — Node.js's original module system, which loads code with require() and exports it with module.exports.
Taught in
- Native Module Systems: ESM Loader Hooks vs CommonJS — Node.js Internals & Backend Services
- Stream Mechanics: Readable Streams & Flowing Modes — Node.js Internals & Backend Services