DEV Community

Cover image for Promise.race vs. Promise.any And Promise.all vs. Promise.allSettled

Promise.race vs. Promise.any And Promise.all vs. Promise.allSettled

Sung M. Kim on May 18, 2019

                    Photo by Ryan Franco on Unsplash What’s new in JavaScript (Google I/O ’19) on May 8, 2019 showed what’s coming/available for s...
Collapse
 
vulpcod3z profile image
vulpz

Great post and explanation! I personally have dreaded not having an alternative to Promise.allSettled() as it has required me to rewrite much of my applications to still work with Promise.all(). It usually happens anytime there's a need to gather finalized data AFTER running async methods from an array.

Collapse
 
dance2die profile image
Sung M. Kim

Thank you, vulpz 😃

rewrite much of my applications to still work with Promise.all()

How did you get around the issue of current short-circuit behavior of .all()? (write a custom .allSettled or used many try/catch blocks? Those are only two I can think of 🤔)

Collapse
 
vulpcod3z profile image
vulpz

Possibly bad form, but I went the route of "misusing" resolve and reject. If there was a bad case that had issues, yet needed to be caught by Promise.all(), I returned an object that used the try/catch but searched for a specific flag.

Not the most efficient, but it worked and was easier to implement than rewriting the majority of the project!

Thread Thread
 
dance2die profile image
Sung M. Kim

Getting things done FTW~

Collapse
 
bingocaller profile image
Jesper N. Jakobsen

Great article! I wasn't even aware of the new Promise features but you did a good job of explaining them.
Also, it looks like you need to swap line 2 and 3 in your Promise.any example output gist so they correspond to the example order. :)

Collapse
 
mayassalman profile image
mayas salman

I think that they correspond to the example order....
It is because of setTimeout(....

Collapse
 
bingocaller profile image
Jesper N. Jakobsen

I agree that they correspond to what output you would get from the code (because of setTimeout()), but I don't agree that they correspond to the example order.
Maybe what I should have said was that to make the example and output more readable, one would swap example 2 and 3. That way the output Gist corresponds to the example order and the actual output of the code.

Thread Thread
 
dance2die profile image
Sung M. Kim • Edited

Thanks Jesper for the suggestion.
I've updated the "output" with comments.

The reason I left the order as it was because I initially expected people to "copy/paste" the code and see the result.
(as opposed to copy/paste each example one at a time)

updated

Behold the power of Gist, now all my x-posts are updated too 😀

I will take notes of your suggestion when I post any types of examples next time.

Collapse
 
iammpp94 profile image
idevthings101@gmail.com

Good post, looking forward to seeing these implemented in all browser vendors

Collapse
 
dance2die profile image
Sung M. Kim

Thanks.
These methods will rid the need for boilterplate codes for sure :)

Collapse
 
davidokonji profile image
David Okonji

This was an awesome article, 👍🏼 really helped understand the different static methods of a promise.

Collapse
 
dance2die profile image
Sung M. Kim

Thanks for the kind words, David. Glad that you liked it :)

Collapse
 
wormss profile image
WORMSS

So, what is the purpose of extractApples method? It seem to be be defined, but never used?

Collapse
 
dance2die profile image
Sung M. Kim

Thank you @wormss for catching the vestigial code and I apologize for getting back late.

I removed extractApples from the code snippet as it's "never used" :)