为什么No-return-await vs const x = await? [英] Why no-return-await vs const x = await?

查看:177
本文介绍了为什么No-return-await 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处理(直接作为Promiseawait -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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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