Skip to content

Promise.all vs Promise.allSettled

Published: at 04:07 PM (2 min read)

Promise.all vs Promise.allSettled


How does Promise.allSettled work?

Promise.allSettled method takes promises as input and returns a single promise. This returned promise is fulfilled when all the input promises settle, with an array of objects with 2 keys, status (status of the promise, resolved or rejected) & value (the rejected reason or resolved value) or if the promsie is rejected the key is reason.

[
  { status: "fulfilled", value: 1 },
  { status: "rejected", reason: Error("Failed") },
];

So the difference between them in terms of rejecting promises is that:


When to use promise.all and promise.allSettled?

For promise.all we use it for tasks that are dependent of each other, if one task fails then we do not want to proceed. But for promise.allSettled, we use it for tasks that are independent of each other, even if one fails we want to handle all of them and show successfull results. For example, in a dashboard where some widgets can load independently, you might want to use Promise.Settled so that available data is displayed even if some sources fail.


References

How does Promise.all() method differs from Promise.allSettled() method in JavaScript ? - GeeksforGeeks Promise.allSettled() - JavaScript | MDN Understanding Promise.all() and Promise.allSettled(): When to Use Each | by Vikrant Bhadauria | Medium Promise.all vs. allSettled: Essential JS Error Handling Differences - ReviewInsights.com


Previous Post
Express.js – What It Is and How It Works
Next Post
Proper Error Handling in Express.js