-2

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

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Sagar Darekar
  • 982
  • 9
  • 14
  • 4
    **Top level** await does not mean you no longer need to mark functions as `async` to use `await` inside them. It only means that you can use `await` in module code. – Bergi Jul 06 '22 at 04:38
  • [What are asynchronous functions in JavaScript? What is "async" and "await" in JavaScript?](https://stackoverflow.com/q/62196932) – VLAZ Jul 06 '22 at 04:45

2 Answers2

0

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.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
-1

If you ever want to use await inside a function.

peter duffy
  • 195
  • 9