在异步函数之外使用 await [英] Using await outside of an async function

查看:33
本文介绍了在异步函数之外使用 await的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将两个异步函数链接在一起,因为第一个有一个条件返回参数,导致第二个要么运行,要么退出模块.但是,我发现了在规范中找不到的奇怪行为.

I was attempting to chain two async functions together, because the first had a conditional return parameter that caused the second to either run, or exit the module. However, I've found odd behavior I can't find in the specs.

async function isInLobby() {
    //promise.all([chained methods here])
    let exit = false;
    if (someCondition) exit = true;
}

这是我代码的一个混蛋片段(你可以看到完整的范围此处),它只是检查玩家是否已经在大厅中,但这无关紧要.

This is a bastardized snippet of my code (you can see the full scope here), that simply checks if a player if already in a lobby, but that's irrelevant.

接下来我们有这个异步函数.

Next we have this async function.

async function countPlayer() {
    const keyLength = await scardAsync(game);
    return keyLength;
}

如果exit === true,这个函数不需要运行.

This function doesn't need to run if exit === true.

我尝试过

const inLobby = await isInLobby();

我希望这会等待结果,所以我可以使用 inLobby 有条件地运行 countPlayer,但是我收到了一个没有具体细节的类型错误.

This I hoped would await to results, so I can use inLobby to conditionally run countPlayer, however I received a typeerror with no specific details.

为什么你不能await一个超出函数范围的async函数?我知道这是一个糖承诺,所以它必须链接到 then 但为什么在 countPlayer 中我可以等待另一个承诺,但在外面,我不能 await isInLobby?

Why can't you await an async function outside of the scope of the function? I know it's a sugar promise, so it must be chained to then but why is it that in countPlayer I can await another promise, but outside, I can't await isInLobby?

推荐答案

不支持顶级 await.标准委员会对此进行了一些讨论,例如 this Github issue.

Top level await is not supported. There are a few discussions by the standards committee on why this is, such as this Github issue.

还有一个 Github 上的想法 说明为什么顶级 await 是个坏主意.具体来说,他建议如果您有这样的代码:

There's also a thinkpiece on Github about why top level await is a bad idea. Specifically he suggests that if you have code like this:

// data.js
const data = await fetch( '/data.json' );
export default data;

现在任何导入data.js的文件在获取完成之前不会执行,因此所有模块加载现在都被阻止.这使得推断应用模块顺序变得非常困难,因为我们习惯于同步和可预测地执行顶级 Javascript.如果允许这样做,则知道何时定义函数变得棘手.

Now any file that imports data.js won't execute until the fetch completes, so all of your module loading is now blocked. This makes it very difficult to reason about app module order, since we're used to top level Javascript executing synchronously and predictably. If this were allowed, knowing when a function gets defined becomes tricky.

我的观点是你的模块仅仅通过加载就产生副作用是不好的做法.这意味着您的模块的任何使用者都会因为需要您的模块而获得副作用.这严重限制了您的模块的使用范围.顶级await 可能意味着您正在加载时从某个API 读取数据或调用某个服务.相反,您应该只导出消费者可以自己使用的异步函数速度.

My perspective is that it's bad practice for your module to have side effects simply by loading it. That means any consumer of your module will get side effects simply by requiring your module. This badly limits where your module can be used. A top level await probably means you're reading from some API or calling to some service at load time. Instead you should just export async functions that consumers can use at their own pace.

这篇关于在异步函数之外使用 await的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆