Rule proposal: `prefer-top-level-await`
See original GitHub issuePrefer top-level await instead of async IIFE or a top-level function call to an async function.
https://github.com/tc39/proposal-top-level-await
Top-level await is supported in Node.js since Node.js v14.3.0. So we cannot yet add it to the recommended preset, but we can add a TODO comment.
The async IIFE pattern is quite popular in command-line apps: https://github.com/sindresorhus/is-online-cli/blob/fb03be268bdec08e264dad31c5e7a110579d02f6/cli.js#L26-L33
Fail
(async () => {
await getUnicorns();
})();
function main() {
await getUnicorns();
}
main();
Pass
await getUnicorns();
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:9 (5 by maintainers)
Top Results From Across the Web
javascript - How can I use async/await at the top level?
In a module, once the top-level `await` proposal lands try { const ... Then node.js will secretly concatenate a function that looks like:...
Read more >Streamline your JavaScript code with top-level await
You can find more uses of top-level await in the ECMAScript TC39 top-level await proposal. The code for this top-level await example using...
Read more >Experiments | webpack
topLevelAwait : Support the Top Level Await Stage 3 proposal, it makes the module an async module when await is used on the...
Read more >Top level `await` are now stage 3 in EcmaScript : r/node - Reddit
Just like regular non-top-level-await does today. ... I would take a look at https://github.com/tc39/proposal-top-level-await/blob/master/README.md#solution ...
Read more >State of Async/Await in JavaScript | by Keerti Kotaru
Read about the TC39 proposal “top-level-await” and its criticism. ... Initialization: Await for initialization of connections to resources like databases.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Summary,
include
Enough?
Stage 4 now https://github.com/tc39/proposals/commit/99fa42a0863b1915eccead5bd2939ed5f468caab