-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

The TypeScript Workshop
By :

Top-level await
is a feature that allows the use of the await
keyword at the module level, outside of any function. This allows a number of interesting patterns, such as waiting for a dependency to fully load by calling an asynchronous function before attempting to use it. Someday, top-level await
may support some very exciting functional programming paradigms, but at the time of writing, it is still technically in preview mode, and so is not ready for widespread use. You may be reading this book at a time when top-level await
is widely available and supported, and if so, you should definitely give it a look!
Writing code with top-level await
is very straightforward. Here is a very short program that attempts to make use of it:
export const fn = async () => { return 'awaited!'; }; console.log(await fn());
This looks fine. Now let's see what happens when we try to execute it:
⨯ Unable to compile TypeScript: src/top-level...