It is amazing that, nodejs developer can accept and work with callback in a long time as it is.

Everyone complain about the callback hell. Look at the picture above and a “sample” code below then will know why it is called as “hell”. It is not only dirty. Problem is how I know where a result or an error from?

function foo(bar,function (result){     // try to play with result with another callback? wth?     function x(result,function (resultOfX){           //play with resultOfX, result of bar now     })})

Then they provide us a new hell level, Promise.

We don’t need to add a function as a paramter to another function. Instead we place it in the tail of a function.

Let see:

function foo(abc).then(function (result){     function x(result).then(function (resultOfX){           //play with resultOfX, result of bar now     })});

It seem better but I still don’t know where an error or a result from when I receive it? still bad, right?

Thank God, we have a new thing: async-await pair. My code now become:

async (() => {    let result = await function foo(abc){         // ....    };    //play with result    let resultOfX = await function x(result){        //....    }    // play with resultOfX})

That so cool. I can track anytime I see an error. And as in another language, when you have one line code, it should return result before you go to the next line.

It is not only one benefit of this angel pair. it also make our code shorter, cleaner. That will make code reading experience more human

So let enjoy it.

Quyet Vu

Written by

Quyet Vu

Sometimes you win, sometimes you learn

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade