DEV Community

Discussion on: JavaScript - Demystifying Callbacks, Promises and Async Functions

Collapse
landb profile image
Branko Stancevic • Edited

Great article! But I have stupid observasion. Doesn’t await blocks the execution of the following code until it gets results?

For e.g.
await bla();
Console.log(“log after await”)

Console log will appear only when bla function is finished with execution, am I right?

If I am right, I don’t think that async is syntetic sugar to promises becase there’s a big difference: blocking the execution of the code with async and non blocking with promises :D

Collapse
vcpablo profile image
Pablo Veiga Author

Hey @landb , you take a closer look to this code example, you'll notice that the console.log will also be executed each time a promise is resolved

orderPizza()
.then(function(pizza) {
  console.log(`A ${pizza.flavour} has been ordered!`)  
  return waitForPizzaToBeDelivered()
})
.then(function() {
  console.log('Pizza delivered!')
  return payForPizza()
})
.then(function() {
  console.log('Pizza paid!')
  return eatPizza()
})
.then(function() {
  console.log('Pizza finished :(')
})
Enter fullscreen mode Exit fullscreen mode

By using async/await in the last example, we will have the same result, but in a more readable way.
This is enough to call it synthetic sugar to promises, at least in this scope.
Perhaps, in other kinds of examples, this may not apply very well.

But I see your point!
Thank you so much for participating.

Cheers!