Using await at the top level in ES modules - Matt Smith
Briefly

ES2022 introduced top-level await, allowing developers to use the await keyword at the top level of ES modules, simplifying asynchronous code without needing async functions or IIFE wrappers. This change enhances readability and reduces boilerplate code. However, top-level await is limited to ES modules and does not work in CommonJS or traditional script tags. Practical applications include fetching remote configurations and dynamic imports, enabling straightforward integration of asynchronous behavior at module level.
Top-level await enables JavaScript developers to use the await keyword directly at the top level of ES modules, simplifying asynchronous coding patterns and eliminating boilerplate.
In the past, developers had to wrap asynchronous logic within an IIFE to use await, but top-level await allows for more straightforward, readable code without nesting.
This feature only works in JavaScript modules and does not apply to CommonJS or traditional script tags, allowing for cleaner integration into modern applications.
Real-world applications of top-level await include fetching remote configuration files at startup and using dynamic imports for conditionally loading modules, enhancing modularity.
Read at Allthingssmitty
[
|
]