DEV Community

Cover image for Understanding Async & Await

Understanding Async & Await

Maxim Orlov on October 07, 2022

This article was originally published at https://maximorlov.com/understanding-async-await/ Learning async/await can be daunting. It's something yo...
Collapse
 
timdenison profile image
Tim Denison

As a younger dev I read a lot of similar descriptions of async/await mechanics but didn’t fully understand, and it was never fully explained, what the value was. Why do I care? What does using this pattern give me, and how does THAT work?

You hint at the value by mentioning that await pauses execution of that particular function, which implies that the thread could go an execute other instructions during that time that the first function execution is paused, but as a younger dev I couldn’t intuit that implication. I wished I had someone offering a mental model for that aspect of asynchronous programming when I was younger.

Collapse
 
brense profile image
Rense Bakker

Very nice detailed explanation of asynchronous JavaScript!

Collapse
 
maximization profile image
Maxim Orlov

Thank you Rense!

Collapse
 
eshimischi profile image
eshimischi

Async/await is just a syntax sugar for Promise/then.

Collapse
 
srnascimento40 profile image
SrNascimento40

Your article clarifies that concept for me, thanks for sharing it with us!

Collapse
 
maximization profile image
Maxim Orlov

Happy to hear it clarified this topic for you!

Collapse
 
azimsabbir profile image
Mohammad Azim Sabbir

thank you very much that was helpful

Collapse
 
maximization profile image
Maxim Orlov

Glad to hear you found it helpful!

Collapse
 
alvesjessica profile image
Jessica Alves

Nice explanation 👏

Collapse
 
maximization profile image
Maxim Orlov

Thanks Jessica!

Collapse
 
cmcoder2012 profile image
CM

The simple explanation from a master of JS guru.

Collapse
 
maximization profile image
Maxim Orlov

That's flattering, thanks 😊. Glad you liked it!

Collapse
 
azzarox profile image
Azzarox

Do I need to always use await when calling the function (to retrieve the value) or I can omit using await if I don't need to use the retrieved value. For example if I have 'guests' in my database and I have function like find() which returns all of my guests (which is promise). Do I need to use await in some other function if I just want to call the function (that retrieves the guests) but I don't need to use the value. I think I know the answer already due to the article but still would like to ask. Great article regardless, one of the best I have read on the subject.

Collapse
 
maximization profile image
Maxim Orlov

Great question! If you're not using the value you don't have to use await. But beware, if the functions throws the error won't be caught in the catch-block. Also any code running after the async function cannot assume the function has completed successfully. Say you run sendNotification() in the backend and then respond to the client that the notification has been sent, well, you don't know that for sure :). Hope that makes sense!

Collapse
 
azzarox profile image
Azzarox

Yes, absolutely! Thank you very much.

Collapse
 
crazyrunsnail profile image
davino

what software you used to draw