简体   繁体   English

应用“无返回等待”时,函数是否应该保持异步?

[英]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?如果第一个例子变成第二个例子,它实际上不应该变成第三个例子吗?

是的,如果函数不返回 await,则使用 async 毫无意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM