question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rule proposal: `prefer-top-level-await`

See original GitHub issue

Prefer 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:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
fiskercommented, Jun 2, 2021

Summary,

  1. Top level Async IIFE
(async function () {})();
(async () => {})();
  1. Top level call
fn(); // `fn` is an async function
  1. Top level promise
anything.{then,catch,finally}();

include

(async () => {}).catch();

Enough?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found