Reputation: 620
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?
Upvotes: 0
Views: 73
Reputation: 75
Yes, there is no point in using async if the function does not return await.
Upvotes: 0
Reputation: 353
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.
Upvotes: 1