Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why no-return-await vs const x = await?

What is the difference between

return await foo() 

and

const t = await foo(); return t 

http://eslint.org/docs/rules/no-return-await

like image 324
AturSams Avatar asked Jun 28 '17 14:06

AturSams


People also ask

Is return await necessary?

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 .

Can we use await in return statement?

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.

Why is async await better than callbacks?

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 .

Why promises are better than async await?

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.


1 Answers

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.

like image 133
samanime Avatar answered Oct 04 '22 05:10

samanime