DEV Community

Don't Make This Async/Await Oopsie!

Neil Syiemlieh on March 25, 2019

Suppose we need to perform some I/O on items of an array, like fetching the owners of cats from the cats' IDs using some API. const catIDs = [13...
Collapse
 
lmorchard profile image
Les Orchard

Calling this an oopsie or an error is a bit unfortunate.

Sometimes you want to make requests serially. This is not a mistake.

Maybe you want to fail or interrupt the whole process based on the result of one of the fetches. You can do that easily in a for loop.

Consider if you had 100 fetches to do - you don't want to fire them all off simultaneously and flood the services you're using. You could take serial chunks of the work in a for loop and fire those off in parallel.

So, good tip with map & Promise.all, but serial execution is not a mistake in and of itself

Collapse
 
mebble profile image
Neil Syiemlieh

Thank you for this comment. I should have stated that this is only something people should be aware of. I went with a clickbaity title just to get it out there. I'll update the article to include this.

Collapse
 
q3flat profile image
q3flat

I was wandering on social media when this article poped up. But had no time to finish this off, therefore this page was opened for about 3 weeks long and now I had the time. I finished it! I don't regret! Very useful article (boomerangs, right? :)). Thank you your effort!

Collapse
 
mebble profile image
Neil Syiemlieh

Thank you for your support!

Collapse
 
rollergui profile image
Guilherme

I've always had some trouble with async/await, some unexpected stuff always happened when using them with map/filter/reduce.
Found some solutions but this is by far the most instructive. Great examples and great first post!

Collapse
 
mebble profile image
Neil Syiemlieh

Thank you! It feels great to hear from someone who found the post helpful

Collapse
 
arya567000 profile image
arya567000

hey Neil, great article, can you recommend me a good best practice article or books about something like this and to learning the mern stack thank you

Collapse
 
mebble profile image
Neil Syiemlieh

Thank you! I haven't worked on JS in a while now, so I don't recall any best practice article or books at the moment. But I did learn a lot from Eloquent JavaScript and Andrew Mead's Udemy courses

Collapse
 
arya567000 profile image
arya567000 • Edited

thanks bud, hope that you find your true life purpose as a good and be better human being.. always remember allah, buddy.

Collapse
 
zeddotes profile image
zeddotes

Thanks for the tip. Could you have used generators, instead, to do the job?

Collapse
 
mebble profile image
Neil Syiemlieh

Async await are based on generators but I haven't used generators themselves as they're not as popular as async await. It would be very interesting to see how generators would be used for async code

Collapse
 
pdamra profile image
Philip Damra

Good tip! Congratulations on your first post!

Collapse
 
mebble profile image
Neil Syiemlieh

Thank you!

Collapse
 
andersjr1984 profile image
andersjr1984

Oh man. This is some good stuff. I feel like I'm a serial async/await abuser.

Collapse
 
daveskull81 profile image
dAVE Inden

Great post. Really interesting to read this approach to async/await and getting code running in parallel. This helps solidify the power of an async/await approach to this kind of process.