为什么不返回等待 vs const x = await? [英] Why no-return-await vs const x = await?

查看:19
本文介绍了为什么不返回等待 vs const x = await?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么区别

return await foo()

const t = await foo();
return t

http://eslint.org/docs/rules/no-return-await

推荐答案

基本上是因为return await是多余的.

Basically, because return await is redundant.

从您实际使用 async 函数的更高层次来看:

Look at it from a slightly higher level of how you actually use an async function:

const myFunc = async () => {
  return await doSomething();
};

await myFunc();

任何 async 函数已经将返回一个 Promise,并且必须作为 Promise 处理(或者直接作为 Promise>Promise,或者也await-ing.

Any async function is already going to return a Promise, and must be dealt with as a Promise (either directly as a Promise, or by also await-ing.

如果你在函数内部await,它是多余的,因为外面的函数也会以某种方式await它,所以没有理由不只发送Promise 并让外部事物处理它.

If you await inside of the function, it's redundant because the function outside will also await it in some way, so there is no reason to not just send the Promise along and let the outer thing deal with it.

它在语法上没有错误或不正确,通常不会引起问题.这完全是多余的,这就是 linter 在其上触发的原因.

It's not syntactically wrong or incorrect and it generally won't cause issues. It's just entirely redundant which is why the linter triggers on it.

这篇关于为什么不返回等待 vs const x = await?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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