If using return await
is unnecessary should the function remain 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
https://eslint.org/docs/rules/no-return-await
Example 1
async function foo() {
return await bar();
}
Example 2
async function foo() {
return bar();
}
Example 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. 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 .
是的,如果函数不返回 await,则使用 async 毫无意义。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.