I am just curious, as per ES2022
release we can use top level await
and that means we don't need to use async
keyword to use await
, before es2022
release we were not allowed to write await
without async
.
So what will be the use of async
keyword now? Is there are any other use cases where we can utilize async
keyword?
Reference - What’s new in JavaScript after the ES2022 release
CodePudding user response:
Top-level await does not replace async functions. You will still (and forever) be forbidden to do
const fn = () => {
await callApi();
};
or anything more complicated than that, because await can only used in an async function - and permitting await
on the top level doesn't change that.
Functions are the building blocks of modular programming. Top-level await is only there to make the syntax a bit easier - it doesn't fundamentally change the language or make functions obsolete.
CodePudding user response:
If you ever want to use await inside a function.