2019-05-18
Promise.race vs. Promise.any And Promise.all vs. Promise.allSettled
javascript
javascript
_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 static Promise combinator methods, Promise.allSettled and Promise.any.
There are already two methods available in modern browsers, Promise.all and Promise.race.
Let's take a look at differences and how each method works.
I will skip on what a promise is and jump straight into static methods and will discuss differences.
A gist is that, a promise is JavaScript's way of promising you that a work will be done (or might fail if the work could not be completed).
If you are familiar with C#, it's analogous Task class.
For more info, refer to following documentations.
For more detailed explanation of states & fates, please refer to States and Fates.
There are other static Promise methods such as Promise.reject, Promise.resolve but I will cover only "combinator" methods, which takes in an iterable object as an argument.
Let's first take a look at difference between existing & new combinator methods.
Both accepts an iterable object but
Both accepts an iterable object but
Now let's take a look at existing/upcoming combinator methods.
https://gist.github.com/dance2die/9d9f7531775efed3eca1ad70bfd17a73
Now let's move on to learn more about each method.
Note that all "Characteristics" are taken from TC39 proposal README.
https://gist.github.com/dance2die/9ba18d739224d408af8a8f239cde9a2b
When Promise.all fulfilled(promisesWithoutReject), all apples are returned.
The latter example using promisesWithOneReject shows that one rejected promise results in rejecting all promises.
https://gist.github.com/dance2die/e6f491a82eb10a28f3743f68f5c9d4fe
Regardless of settled (fulfilled or rejected) state, all promises resolve without short-circuiting to catch.
To differentiate if resolved values were successful, they are returned as an array of objects of following shape.
https://gist.github.com/dance2die/e59a0f90b6a0ea585520b994652bfb55
In promiseWillFulfill example, the first promise fulfilled within 1 millisecond and thus the humanity survived.
But the second example using promiseWillReject had a promise rejecting in 1 millisecond and thus the humanity is doomed.
And the last example (promisesWithOUTReject) fulfilled without rejection thus the first fulfilled promise value of "
three" was returned.
From these examples, you can see that the first settled state (fulfilled or reject) short circuited the promise.
https://gist.github.com/dance2die/992a1157c191eca2b277e1380b323e8b
First example has promises that rejects right away but did not short-circuit because of a fulfilled promise, thus you win at life.
Second example has promises resolving after a certain period. The first fulfilled promise was resolved after a series of rejects but didn't short-circuit. And you were able to get a job.
When all promises reject, then that's when Promise.any rejects and you didn't get any job offers.
How I understood was that the new Promise.allSettled/any are introduced for Promise to try its best to resolve promises to fulfill unlike existing ones that fails on first encounter of reject.
Promise.all & Promise.race has been available in modern browsers (this exclude IE ;p) and Promise.allSettled will be available in Chrome 76.
Promise.any is still in stage 1 and not available in any browsers (but available in Bluebird or using polyfills - for the demo I used promise-any NPM library for demo.)
I'd love to hear where you would (have) use(d) each method to solve a problem.
And would you please kindly let me know if you find any mistakes and/or how I can improve the example?