DEV Community

loading...

Understanding Promise.all, Promise.race and Promise.allSettled

The Nerdy Dev
Building products is my profession, Creating content is my passion. We believe that Education should be freely accessible to everyone and no one should be deprived of it.
・2 min read

Hey everyone 👋🏻,

In this article, let us discuss some very important methods that we have in the Promise API - Promise.all,Promise.allSettled, Promise.race.
So let's start with Promise.all

Promise.all

The Promise.all() method takes an iterable of promises as an input, and returns a single Promise that resolves to an array of the results of the input promises. This returned promise will resolve when all of the input's promises have resolved, or if the input iterable contains no promises. It rejects immediately upon any of the input promises rejecting or non-promises throwing an error, and will reject with this first rejection message / error.

Alt Text

Promise.allSettled

The Promise.allSettled() method returns a promise that resolves after all of the given promises have either fulfilled or rejected, with an array of objects that each describes the outcome of each promise.

It is typically used when you have multiple asynchronous tasks that are not dependent on one another to complete successfully, or you'd always like to know the result of each promise.

In comparison, the Promise returned by Promise.all() may be more appropriate if the tasks are dependent on each other / if you'd like to immediately reject upon any of them rejecting.

Alt Text

Promise.race

The Promise.race() method returns a promise that fulfills or rejects as soon as one of the promises in an iterable fulfills or rejects, with the value or reason from that promise.

Alt Text

So this is it for this article, I do have a video that covers the same with a lot of interesting examples, so feel free to check that :

If you are looking to learn Web Development, I have curated a FREE course for you on my YouTube Channel, check the below article :

If you have spare 2 hours, then utilize them by creating these 10 JavaScript Projects in under 2 Hours

👉🏻 Follow me on Twitter : https://twitter.com/The_Nerdy_Dev

👉🏻 Check out my YouTube Channel : https://youtube.com/thenerdydev

Discussion (0)