“返回等待”是否存在性能问题? [英] Are there performance concerns with `return await`?

查看:280
本文介绍了“返回等待”是否存在性能问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到 eslint rule, no-return -await ,禁止返回等待

I see there is an eslint rule, no-return-await, for disallowing return await.

在规则中描述,它声明返回等待添加在总体Promise解析或拒绝之前的额外时间

In the rule's description, it states a return await adds "extra time before the overarching Promise resolves or rejects".

但是,当我看到 MDN async function docs ,简单示例显示包含返回等待的示例没有任何描述为什么这可能是性能问题。

However, when I look at MDN async function docs, the "Simple Example" shows an example containing return await without any description of why this might be a performance problem.

是否返回等待实际性能问题,因为eslint文档建议?

Is return await an actual performance problem as the eslint docs suggest?

如果是这样,怎么样?

推荐答案

不,没有任何性能问题。这只是一个不必要的额外操作。执行可能需要更长的时间,但应该难以察觉。它类似于返回x + 0 而不是返回x 表示整数 x 。或者更确切地说,完全等同于毫无意义的 .then(x => x)

No, there isn't any performance problem. It's just an unnecessary extra operation. It might take a bit longer to execute, but should be hardly noticeable. It's akin to return x+0 instead of return x for an integer x. Or rather, exactly equivalent to the pointless .then(x => x).

它没有造成实际的伤害,但我认为它的风格很糟糕,并且表明作者没有完全克服和害羞;遵守承诺和 async / await

It doesn't do actual harm, but I'd consider it bad style and a sign that the author does not fully compre­hend promises and async/await.

然而,有一个案例它产生了重要的区别:

However, there's one case where it make an important difference:

try {
    …
    return await …;
} …

await 抛弃拒绝,并且在任何情况下等待承诺解决,然后执行 catch 最后处理程序。普通的返回会忽略它。

await does throw on rejections, and in any case awaits the promise resolution before catch or finally handlers are executed. A plain return would have ignored that.

这篇关于“返回等待”是否存在性能问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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