Top-Level Await
Top-Level Await
Section titled “Top-Level Await”Introduction
Section titled “Introduction”Top-level await allows you to use await at the top level of modules without wrapping it in an async function.
Basic Usage
Section titled “Basic Usage”// In a module — no async function needed!const response = await fetch('/api/config');const config = await response.json();console.log(config);Practical Example
Section titled “Practical Example”export const config = await fetch('/api/config').then(r => r.json());// Module waits for this to resolve before its imports resolve
// main.jsimport { config } from './config.js';console.log(config); // Already loaded!Important Notes
Section titled “Important Notes”- Only works in ES modules (not classic scripts)
- Blocks the module’s execution (not the entire app)
- Other modules importing this module will wait
- Use with care — it delays module evaluation
Summary
Section titled “Summary”awaitat the top level of modules- No
asyncwrapper needed - Only in ES modules
- Blocks the importing module chain