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
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!`)returnwaitForPizzaToBeDelivered()}).then(function(){console.log('Pizza delivered!')returnpayForPizza()}).then(function(){console.log('Pizza paid!')returneatPizza()}).then(function(){console.log('Pizza finished :(')})
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!
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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
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 resolvedBy 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!