问题描述
两者之间有什么区别
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
或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.
这在语法上不是错误或不正确,并且通常不会引起问题.这完全是多余的,这就是棉短绒触发它的原因.
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.
这篇关于为什么No-return-await vs const x = await?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!