What is the difference between
return await foo()
and
const t = await foo(); return t
http://eslint.org/docs/rules/no-return-await
However, if you want to catch the rejected promise you're returning from an asynchronous function, then you should definitely use return await promise expression and add deliberately the await .
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.
The await keyword is used in an async function to ensure that all promises returned in the async function are synchronized, ie. they wait for each other. Await eliminates the use of callbacks in .
Promise is an object representing intermediate state of operation which is guaranteed to complete its execution at some point in future. Async/Await is a syntactic sugar for promises, a wrapper making the code execute more synchronously. 2. Promise has 3 states – resolved, rejected and pending.
Basically, because return await
is redundant.
Look at it from a slightly higher level of how you actually use an async
function:
const myFunc = async () => { return await doSomething(); }; await myFunc();
Any async
function is already going to return a Promise
, and must be dealt with as a Promise
(either directly as a Promise
, or by also await
-ing.
If you await
inside of the function, it's redundant because the function outside will also await
it in some way, so there is no reason to not just send the Promise
along and let the outer thing deal with it.
It's not syntactically wrong or incorrect and it generally won't cause issues. It's just entirely redundant which is why the linter triggers on it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With