在某些情况下省略“等待”是否合法? [英] Is it legitimate to omit the 'await' in some cases?

查看:110
本文介绍了在某些情况下省略“等待”是否合法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的代码中的几个地方使用 async / 等待

I am using async/await in several places in my code.

例如,如果我有这个函数:

For example, if I have this function:

async function func(x) {
    ...
    return y;
}

然后我总是这样称呼:

async function func2(x) {
    let y = await func(x);
    ...
}

我注意到在某些情况下,我可以省略等待,程序仍然可以正常运行,所以我不知道何时必须使用 await 和什么时候我可以放弃它。

I have noticed that in some cases, I can omit the await and the program will still run correctly, so I cannot quite figure out when I must use await and when I can drop it.

我已经得出结论认为仅仅直接放弃 await 是合法的返回声明。

I have concluded that it is "legitimate" to drop the await only directly within a return statement.

例如:

async function func2(x) {
    ...
    return func(x); // instead of return await func(x);
}

这个结论是否正确,否则,我在这里缺少什么?

Is this conclusion correct, or else, what am I missing here?

编辑:

一个未提及的小(但重要)概念以下任何答案,我刚刚遇到并意识到:

A small (but important) notion that has not been mentioned in any of the answers below, which I have just encountered and realized:

放弃 await return 语句中,如果被调用的函数可能抛出异常,那么该语句在中执行阻止。

It is NOT "legitimate" to drop the await within a return statement, if the called function may throw an exception, and that statement is therefore executed inside a try block.

例如,在下面的代码中删除等待是危险的:

For example, removing the await in the code below is "dangerous":

async function func1() {
    try {
        return await func2();
    }
    catch (error) {
        return something_else;
    }
}

原因是尝试块完成而没有异常, Promise 对象返回正常。但是,在调用外部函数的任何函数中,当 Promise 对象被执行时,将发生实际错误并抛出异常。仅当使用 await 时,才会在外部函数中成功处理此异常。否则,该责任会增加,其中需要额外的尝试 / catch 子句。

The reason is that the try block completes without an exception, and the Promise object returns "normally". In any function which calls the outer function, however, when this Promise object is "executed", the actual error will occur and an exception will be thrown. This exception will be handled successfully in the outer function only if await is used. Otherwise, that responsibility goes up, where an additional try/catch clause will be required.

推荐答案

如果 func 是异步函数,则使用和不使用调用它await 有不同的效果。

If func is an async function then calling it with and without await has different effects.

async function func(x) {
  return x;
}

let y = await func(1); // 1
let z = func(1) // Promise (resolves to 1)

省略 await 关键字总是合法的,但意味着你必须处理传统风格的承诺(首先要打败异步/等待点) )。

It is always legitimate to omit the await keyword, but means you will have to handle the promises in the traditional style instead (defeating the point of async/await in the first place).

func(1).then(z => /* use z here */)

如果您的退货单使用 await ,那么您可以确定它会抛出一个错误,它可以在你的函数中捕获,而不是通过调用它的代码。

If your return statements use await then you can be sure that if it throws an error it can be caught inside your function, rather than by the code that calls it.

这篇关于在某些情况下省略“等待”是否合法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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