Language concepts

Top-level await

Try the free JavaScript compiler

Suspends the evaluation of an ES module until the awaited result is available. Importers depending on its successful evaluation wait; a rejected awaited operation can fail the module evaluation. Multi-file example: save each listed file separately, then run the entry file with Node.js. See the packaged fixture for the original files.

Formal Syntax & Structure

javascript syntaxjavascript
export const value = await promise;

Working Example

Example Snippetjavascript
// File: config.mjsconsole.log("config start");export const rate = await Promise.resolve(4);console.log("config ready");
// File: main.mjsimport { rate } from "./config.mjs";console.log("entry", rate);
JavaScript Reference