ES2022版本后,ASYNC关键字将是什么?

发布于 2025-02-13 19:48:43 字数 448 浏览 1 评论 0原文

我只是很好奇,根据es2022发布我们可以使用顶级等待,这意味着我们不需要使用async使用的关键字来使用等待,在ES2022发布之前,我们不允许我们编写等待没有async
那么,现在使用async关键字是什么?是否还有其他用例可以使用async关键字?

参考 -

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执笔绘流年 2025-02-20 19:48:43

顶级等待不是替换异步函数。您仍然会(永远)(永远)

const fn = () => {
  await callApi();
};

这样做或更复杂的事情,因为等待只能在异步函数中使用 - 允许在顶层等待等待 不会改变这一点。

功能是模块化编程的基础。顶级等待着,只是在那里使语法变得更加容易 - 它不会从根本上改变语言或使函数过时。

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.

桃扇骨 2025-02-20 19:48:43

如果您想在功能中使用等待。

If you ever want to use await inside a function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文