ยท new features
Top-level await in TypeScript 3.8
TypeScript 3.8 introduces a new feature called top-level await. It allows developers to use the `await` keyword without a surrounding `async` function at the top level of a module.
TypeScript 3.8 introduces a new functionality which is called top-level await. It allows developers to use the await
keyword without a surrounding async
function at the top level of a module.
Contents
Tutorial
Top-level await was originally planned to land in TypeScript 3.7 but was moved to the iteration plan of TypeScript 3.8. The feature itself is the result of the corresponding ECMAScript proposal called "Top-Level Await".
The following video shows how to use top-level await
in combination with Node.js v13:
Code
Fallback
There is this handy snippet of a main function if you cannot use top-level await for any reason:
TL;DR
To use top-level await in TypeScript, you have to set "target" to es2017
or higher. The "module" option in "tsconfig.json" has to be set to esnext
or system
. Your code also has to run on Node.js v14.8.0 or later.