no-return-await
禁止不必要的 return await
此规则报告的一些问题可通过编辑器 建议 手动修复
This rule was deprecated in ESLint v8.46.0. There is no replacement rule.
不建议再使用 no-return-await
规则,因为:
¥It is NOT recommended to use the no-return-await
rule anymore because:
-
在 Promise 上使用
return await
不会导致额外的微任务。¥
return await
on a promise will not result in an extra microtask. -
return await
能提供更好的堆栈跟踪,方便调试。¥
return await
yields a better stack trace for debugging.
历史背景:当 Promise 首次引入时,调用 return await
会引入一个额外的微任务,一个用于 await
,另一个用于异步函数的返回值。每个额外的微任务都会延迟结果的计算,因此添加了此规则以帮助避免这种性能陷阱。后来,ECMA-262 更改了方式 return await
可以正常工作,因此它会创建一个微任务,这意味着不再需要此规则。
¥Historical context: When promises were first introduced, calling return await
introduced an additional microtask, one for the await
and one for the return value of the async function. Each extra microtask delays the computation of a result and so this rule was added to help avoid this performance trap. Later, ECMA-262 changed the way return await
worked so it would create a single microtask, which means this rule is no longer necessary.
规则详情
¥Rule Details
此规则会对除 try
块之外的任何 return await
使用情况发出警告。
¥This rule warns on any usage of return await
except in try
blocks.
此规则的错误代码示例:
¥Examples of incorrect code for this rule:
/*eslint no-return-await: "error"*/
async function foo() {
return await bar();
}
此规则的正确代码示例:
¥Examples of correct code for this rule:
/*eslint no-return-await: "error"*/
async function foo1() {
return bar();
}
async function foo2() {
await bar();
return;
}
// This is essentially the same as `return await bar();`, but the rule checks only `await` in `return` statements
async function foo3() {
const x = await bar();
return x;
}
// In this example the `await` is necessary to be able to catch errors thrown from `bar()`
async function foo4() {
try {
return await bar();
} catch (error) {}
}
何时不使用
¥When Not To Use It
你不应使用此规则。没有理由避免使用 return await
。
¥You should not use this rule. There is no reason to avoid return await
.
版本
此规则是在 ESLint v3.10.0 中引入。