One thing that is worth mention is that callback hell can be avoided even if you are "stuck" in an environment that doesn't support promises or async/await. You can always use a function reference instead of an anonymous function.
The game example could look like this:
// ...varlevelThreeReached=function(value){console.log('Level Three reached! New score is '+value);}functionlevelTwoReached(value){console.log('Level Two reached! New score is '+value);levelThree(value,levelThreeReached);}functionlevelOneReached(value){console.log('Level One reached! New score is '+value);levelTwo(value,levelTwoReached);}functionstartGame(){varcurrentScore=5;console.log('Game Started! Current score is '+currentScore);levelOne(currentScore,levelOneReached);}
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.
I've been struggling to wrap my head around promises and this article made it very clear with your example. Time to go write something with async/await 😀
Thanks for the article! However, a high-order function is a function that accepts another function as its parameter or returns one. A callback function is a function that gets passed as a parameter.
Hi Alwyn, the examples are asynchronously executed indeed, just that for the simplicity's sake our example level functions call the callbacks instantly. I have not introduced a setTimeout or a ajax call inside the functions but you can play around and see that even if you do, they work perfectly.
Here's an example demonstrating that
functionlevelOne(value,callback){varnewScore=value+5;// making API call to slower server with 5 sec. delayfetch('https://www.mocky.io/v2/5b5c746332000081004262fd?mocky-delay=5000ms').then(function(response){returnresponse.json();}).then(function(myJson){console.log(`// delay on async api call for ${myJson.delay}`);callback(newScore);});// p.s. although setTimeout makes your code behave like async(), it is not.// read more => https://softwareengineering.stackexchange.com/q/194580/}functionstartGame(){varcurrentScore=5;console.log(`Game Started! Current score is ${currentScore}`);levelOne(currentScore,function(levelOneReturnedValue){console.log(`Level One reached! New score is ${levelOneReturnedValue}`);});console.log(`Hi, I'm a rebel and I don't wait for levelOne`);}startGame();// Game Started! Current score is 5// Hi, I'm a rebel and I don't wait for levelOne// // delay on async api call for 5 seconds// Level One reached! New score is 10
I realize I'm a bit late to the discussion, but, since this is a fairly popular article and I believe many will be using it as a reference in the future, I chose to write this comment anyway.
Also, keep in mind, I am far from a JS expert, it is entirely possible for me to get something wrong or misinterpret the original article. Then again, if I misinterpreted it, who knows how many others might misinterpret it as well.
With all that said, Alwyn is 100% percent right, the callbacks in the article are not asynchronous and, if you think about it, they really shouldn't be. As stated in the article, a callback is simply a function passed as an argument to another function. It's just like any other argument a function can get. It makes no sense for a callback to be magically asynchronously called just by being there. We would not expect an argument of type Number to make anything async, so why should a function be any different?
Siwalik, all that is called asynchronously in the code you provided as an answer to Alwyn's question is the first argument of the setTimeout function. This function creates an event and attaches the arrow function to it. This arrow function is then called after the event is settled (5 seconds later). The functions startGame, levelOne and callback are all called synchronously. The only thing making the code appear asynchronous is the event you created with setTimeout and this has nothing to do with callbacks.
A good example of proving the synchronous and disproving the asynchronous nature of your original functions is this one:
functionlevelOne(value,callback){varnewScore=value+5;callback(newScore);console.log('After callback');}functionstartGame(){varcurrentScore=5;console.log('Game Started! Current score is '+currentScore);levelOne(currentScore,function(levelOneReturnedValue){console.log('Level One reached! New score is '+levelOneReturnedValue);// some useless job to cause waitingvara;for(vari=0;i<1000000000;i++){a+=i;}console.log('Done with callback');});console.log('After levelOne');}startGame();
Notice how 'After callback' and 'Done with callback' are printed before 'After levelOne', proving that the callback call is indeed synchronous.
Turns out this 'callbacks being asynchronous' thing is a common misconception. Take a look at this stack overflow thread for more information.
Again, sorry if you knew all of this and I just misunderstood your words.
Hi @Filip, I appreciate you for taking the time and explaining this in detail.
I realise setTimeout isn't asynchronous but I've naively used it to mock how an async call would actually behave, didn't realise people starting out might get confused thinking setTimeout has to do anything with async.
Update: I went up and replaced the setTimeout I wrote in @Alwin's reply with an actual API call with delay, for correct depiction of javascript's working. :)
Pellentesque nec neque ex. Aliquam at quam vitae lacus convallis pulvinar. Mauris vitae ullamcorper lacus. Cras nisi dui, faucibus non dolor quis, volutpat euismod massa. Donec et pulvinar erat.
I'm an experienced full stack developer with a frontend focus, backed by a strong track record of crafting robust and elegant code. Passionate, dedicated, and motivated, I'm ready to take on the next
Nice article. Good job.
One thing that is worth mention is that callback hell can be avoided even if you are "stuck" in an environment that doesn't support promises or async/await. You can always use a function reference instead of an anonymous function.
The game example could look like this:
As someone who doesn't regularly code JS, this was a very helpful primer for these features. Thanks!
Glad to hear that Zeke! 🙂
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
Promises explicitly here to illustrate the point of being able to "get the value out of" thePromiseusing theawaitkeyword, 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
awaita regular value just fine (i.e.let x = await 5is 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
levelfunctionsasyncas well. Anasyncfunction will always return aPromiseimplicitly, 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
Promiseit 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
waitin each case).Note that if you were to remove the
awaitkeywords in thelevelfunctions, that would take away the 500ms pause in that spot. Thewaitfunction would still be called, but the engine is not instructed to wait for the returnedPromiseto 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!
I've been struggling to wrap my head around promises and this article made it very clear with your example. Time to go write something with async/await 😀
Thank you Andrew, good luck ✌️
To anyone exploring asynchronous Javascript programming, also take a look at Observables, in the Rxjs library.
Good and perfect!
Thanks José 🙌
Well done!
Thank you 🙌
Nice article, very succinct and concise.
Yay! Learnt a new word, now I have to use it! 🎉🎉
Glad you found it succinct Luis 🤓
I love this post .. Everything is clear. Thanks
Thanks for the article! However, a high-order function is a function that accepts another function as its parameter or returns one. A callback function is a function that gets passed as a parameter.
Hey varsanyid, nice catch! 🙌Corrected the typo, thanks for pointing 😀
Your callback examples are not async or am I missing something?
Hi Alwyn, the examples are asynchronously executed indeed, just that for the simplicity's sake our example level functions call the callbacks instantly. I have not introduced a setTimeout or a ajax call inside the functions but you can play around and see that even if you do, they work perfectly.
Here's an example demonstrating that
I realize I'm a bit late to the discussion, but, since this is a fairly popular article and I believe many will be using it as a reference in the future, I chose to write this comment anyway.
Also, keep in mind, I am far from a JS expert, it is entirely possible for me to get something wrong or misinterpret the original article. Then again, if I misinterpreted it, who knows how many others might misinterpret it as well.
With all that said, Alwyn is 100% percent right, the callbacks in the article are not asynchronous and, if you think about it, they really shouldn't be. As stated in the article, a callback is simply a function passed as an argument to another function. It's just like any other argument a function can get. It makes no sense for a callback to be magically asynchronously called just by being there. We would not expect an argument of type
Numberto make anything async, so why should a function be any different?Siwalik, all that is called asynchronously in the code you provided as an answer to Alwyn's question is the first argument of the
setTimeoutfunction. This function creates an event and attaches the arrow function to it. This arrow function is then called after the event is settled (5 seconds later). The functionsstartGame,levelOneandcallbackare all called synchronously. The only thing making the code appear asynchronous is the event you created withsetTimeoutand this has nothing to do with callbacks.A good example of proving the synchronous and disproving the asynchronous nature of your original functions is this one:
Notice how
'After callback'and'Done with callback'are printed before'After levelOne', proving that the callback call is indeed synchronous.Turns out this 'callbacks being asynchronous' thing is a common misconception. Take a look at this stack overflow thread for more information.
Again, sorry if you knew all of this and I just misunderstood your words.
Hi @Filip, I appreciate you for taking the time and explaining this in detail.
I realise setTimeout isn't asynchronous but I've naively used it to mock how an async call would actually behave, didn't realise people starting out might get confused thinking setTimeout has to do anything with async.
Update: I went up and replaced the setTimeout I wrote in @Alwin's reply with an actual API call with delay, for correct depiction of javascript's working. :)
Do not forget to write code in es7+ way :-D Great article.
You have explained this really well. 👌
Thank you Sunil 🙌
Pretty decent post. Actually I've never see any cleaner guide to resolve callback helll like this. Kudos!
Appreciate that Tu! Thanks 🙌
Thank you for this well written article. I am sure many will find it useful.
Thanks Jochem! :)
Really useful! Thank you!
I'm happy that you found this helpful.
cool. Thanks