[英]Should a function remain async when applying 'no-return-await'?
If using return await
is unnecessary should the function remain async
?如果不需要使用
return await
函数是否应该保持async
?
Using return await inside an async function keeps the current function in the call stack until the Promise that is being awaited has resolved, at the cost of an extra microtask before resolving the outer Promise
在异步函数中使用 return await 将当前函数保留在调用堆栈中,直到正在等待的 Promise 解决,代价是在解决外部 Promise 之前需要额外的微任务
https://eslint.org/docs/rules/no-return-await https://eslint.org/docs/rules/no-return-await
Example 1示例 1
async function foo() {
return await bar();
}
Example 2示例 2
async function foo() {
return bar();
}
Example 3示例 3
function foo() {
return bar();
}
Bar酒吧
async function bar() {
const data = await externalCall();
const result = anotherFunction(data);
return result;
}
If the first example becomes the second example shouldn't it actually become the third example?如果第一个例子变成第二个例子,它实际上不应该变成第三个例子吗?
I don't think example 3 is the same as example 2. Async ensures that the function returns a promise, and wraps non-promises in it.我不认为示例 3 与示例 2 相同。 Async 确保函数返回一个承诺,并在其中包装非承诺。 See https://javascript.info/async-await#:~:text=Async%20functions&text=The%20word%20%E2%80%9Casync%E2%80%9D%20before%20a,in%20a%20resolved%20promise%20automatically.&text=So%2C%20async%20ensures%20that%20the,wraps%20non%2Dpromises%20in%20it .
参见https://javascript.info/async-await#:~:text=Async%20functions&text=The%20word%20%E2%80%9Casync%E2%80%9D%20before%20a,in%20a%20resolved% 20promise%20automatically.&text=So%2C%20async%20ensures%20that%20the,wraps%20non%2Dpromises%20in%20it 。
是的,如果函数不返回 await,则使用 async 毫无意义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.