Skip to content
Advertisement

Why does the callback hell works and the async/await doesnt? (in this particular case that i am testing)

I have this two codes (one i use callback hell and other async/await):

JavaScript

The callback hell works as i imagined (the string logs appear lastly).

Then i tried to convert the same code but with async/await, like this:

JavaScript

Now the first await blocks the main thread while the other two keeps showing lastly. What am i doing wrong?

Edit with @Muhammad Saquib Shaikh solution:

JavaScript

It is not the same output as the first one.

Advertisement

Answer

Now the first await blocks the main thread while the other two keeps showing lastly. What am i doing wrong?

The reason that ameno gets logged up front is that you have that log statement before any awaits. To evaluate the line await consoleMusical("ameno"); it has to execute consoleMusical('ameno'), get its return value, and only then can it await.

The other console.logs happen after the await, so they will get queued up as microtasks to run after the rest of the currently-executing code.

In contrast, all of the console.logs of callBackHell are inside the first promise. So all of them will be queued up as microtasks.


This problem will go away if you use an asynchronous sleep rather than a synchronous one. And as a bonus, you don’t lock up the browser:

JavaScript

If you absolutely must have a synchronous sleep, while still wanting to keep the same order of operations, you’ll need to add an additional await.

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement