Questions tagged [top-level-await]

Top-level await is a ECMAScript language feature that enables developers to use the await keyword outside of async functions. It acts like a big async function causing other modules who import them to wait before they start evaluating their body.

As of May 2021, all major browsers and NodeJS support Top-level await.

Example

await Promise.resolve(console.log(''));

Without top-level await support, the above line produces the following error:

SyntaxError: await is only valid in async function

Workaround

Without top-level await support, the below boilerplate would typically be used by developers to workaround the lack of top-level await support.

(async function() {
  await Promise.resolve(console.log(''));
}());

History

The proposal for this ECMAScript language feature was initially submitted in January 2018 by Myles Borins and reached TC39 Stage 4 in May 2021.

All major browsers added support for this feature in the beginning of 2021. NodeJS added experimental support for this feature in 14.3.0 and full support in 14.8.0.

25 questions
49
votes
4 answers

Nodejs, TypeScript, ts-node-dev & top-level await

I have spent an hour now searching for a solution to a new error after needing top-level await. Everything else I have tried so far did not solve the error such as adding "type": "module" to the package.json file. The message of the error is Cannot…
treeden
  • 675
  • 1
  • 6
  • 17
21
votes
2 answers

TS Jest won't accept top-level-awaits with NodeJS 16 & TypeScript

I'm trying to update my NodeJS 12 & TypeScript app to Node16, one of the reasons is the need to use top-level-awaits. The code compiles correctly after the update, but Jest won't accept the specific top-level-await code: ts-jest[ts-compiler] (WARN)…
galah92
  • 3,621
  • 2
  • 29
  • 55
12
votes
1 answer

Top-level await and Import in Typescript

I'm studying Typescript and running into a problem. I want to use the import and top-level await but currently, I can only use one at a time. Here is my config in tsconfig.json which allows me to use import "target": "ESNext", "module":…
Nguyen Hoang Vu
  • 751
  • 2
  • 14
  • 35
12
votes
2 answers

Top-level await does not work with node 14.13.-

I have node 14.13.0, and even with --harmony-top-level-await, top-level await is not working. $ cat i.js const l = await Promise.new(r => r("foo")) console.log(l) $ node -v v14.13.0 $ node --harmony-top-level-await i.js /Users/karel/i.js:1 const l…
Karel Bílek
  • 36,467
  • 31
  • 94
  • 149
6
votes
3 answers

Use a top-level await, if supported by the current runtime

Top-level await support was added to Node.js in 14.3.0 via --experimental-top-level-await and later to --harmony-top-level-await. The Problem I need to use a top level await in my ESM script file, if it is supported by the current Node.js runtime.…
concision
  • 6,029
  • 11
  • 29
4
votes
2 answers

Vue3 top-level await Eslint fails

Following the official documentation: https://vuejs.org/api/sfc-script-setup.html#top-level-await I'm trying to create an async component like that: