When returning from a promise from an asynchronous function, you can wait for that promise to resolve return await promise, or you can return it direc

'return await promise' vs 'return promise' in JavaScript

submited by
Style Pass
2021-08-21 17:00:06

When returning from a promise from an asynchronous function, you can wait for that promise to resolve return await promise, or you can return it directly return promise:

To find the difference between the 2 expressions (return await promise vs return promise), I’m going to use a helper function delayedDivide(n1, n2).

The following function divideWithAwait() uses return await promisedDivision(6, 2) expression to return the division of 6 by 2 wrapped in a promise:

Now let’s try to use the second expression without the await keyword, and return directly the promise wrapping the division result return promisedDivision(6, 2):

Even without using the await keyword inside divideWithoutAwait(), the expression await divideWithoutAwait() inside the run() function still evaluates correctly to the 6 / 2 division as 3!

At this step, you have seen that using return await promise and return promise don’t differ. At least when dealing with successfully fulfilled promises.

Leave a Comment