I think the author was returning those Promises explicitly here to illustrate the point of being able to "get the value out of" the Promise using the await keyword, without having to jump through the normal .then(fn) hoops. An alternative (usually considered to be more idiomatic) way to write the return statements would have been return Promise.resolve(newScore);.
However, without any "real" async logic in those functions, you could indeed simply return and it would preserve the current behaviour; you can await a regular value just fine (i.e. let x = await 5 is valid syntax). As far as I'm aware it can potentially affect the order in which things are processed within the current iteration of the event loop, but that won't observably influence things in most cases, including this one.
For completeness' sake, we could take things one step further by making the level functions async as well. An async function will always return a Promise implicitly, so the following are functionally equivalent:
This way, we can leave the explicit Promises out, but still benefit from the asynchronous flow.
To make the asynchronous behaviour clearer, we could introduce a "wait" function as well, which does nothing more than resolve the Promise it returns after the passed amount of milliseconds:
// note: there is no need to mark this function `async`,// as we need to explicitly return a `Promise` anyway in// order to make use of `resolve`functionwait(ms){returnnewPromise(function(resolve){setTimeout(resolve,ms);});}asyncfunctionlevelOne(value){awaitwait(500);returnvalue+5;}asyncfunctionlevelTwo(value){awaitwait(500);returnvalue+10;}asyncfunctionlevelThree(value){awaitwait(500);returnvalue+30;}asyncfunctionstartGame(){letcurrentScore=5;console.log('Game Started! Current score is '+currentScore);currentScore=awaitlevelOne(currentScore);console.log('You have reached Level One! New score is '+currentScore);currentScore=awaitlevelTwo(currentScore);console.log('You have reached Level Two! New score is '+currentScore);currentScore=awaitlevelThree(currentScore);console.log('You have reached Level Three! New score is '+currentScore);}startGame();
If you run this code (e.g. paste it into your console and press enter), you'll see that it runs just as the code in the article, the difference being it has 500ms pauses in between the log messages (the number 500 coming from the value that is passed to wait in each case).
Note that if you were to remove the await keywords in the level functions, that would take away the 500ms pause in that spot. The wait function would still be called, but the engine is not instructed to wait for the returned Promise to resolve before continuing, so it will just move on to the next statement immediately.
In the last example, do you really need
in each function, or you can simply return?
By the way, great article!
I think the author was returning those
Promise
s explicitly here to illustrate the point of being able to "get the value out of" thePromise
using theawait
keyword, without having to jump through the normal.then(fn)
hoops. An alternative (usually considered to be more idiomatic) way to write the return statements would have beenreturn Promise.resolve(newScore);
.However, without any "real" async logic in those functions, you could indeed simply return and it would preserve the current behaviour; you can
await
a regular value just fine (i.e.let x = await 5
is valid syntax). As far as I'm aware it can potentially affect the order in which things are processed within the current iteration of the event loop, but that won't observably influence things in most cases, including this one.For completeness' sake, we could take things one step further by making the
level
functionsasync
as well. Anasync
function will always return aPromise
implicitly, so the following are functionally equivalent:This way, we can leave the explicit
Promise
s out, but still benefit from the asynchronous flow.To make the asynchronous behaviour clearer, we could introduce a "wait" function as well, which does nothing more than resolve the
Promise
it returns after the passed amount of milliseconds:If you run this code (e.g. paste it into your console and press enter), you'll see that it runs just as the code in the article, the difference being it has 500ms pauses in between the log messages (the number 500 coming from the value that is passed to
wait
in each case).Note that if you were to remove the
await
keywords in thelevel
functions, that would take away the 500ms pause in that spot. Thewait
function would still be called, but the engine is not instructed to wait for the returnedPromise
to resolve before continuing, so it will just move on to the next statement immediately.I couldn't have explained this question better @Joep πThank you π
My pleasure!