0

It seems Promise.all() resolves as soon as one of the promises fail, I would like to run all promises even if some reject. Is there a function for this?

  • `Promise.all` does not "stop" promises from running after one fails. Actually `Promise.all` doesn't *run* them at all, it just *waits* for them. – Bergi Apr 29 '20 at 17:54
  • 2
    @gelliott181 https://stackoverflow.com/a/56255129/1048572 – Bergi Apr 29 '20 at 18:15
  • @Bergi That answer is still out of date as it's no longer a proposal. ES2020 has ratified it as a standard feature. I've removed my comment anyways as it's inappropriate to answer in comments and Fullstack Guy has more perms to answer closed questions than I do. – gelliott181 Apr 29 '20 at 18:45
  • @gelliott181 He doesn't have more permissions, he just was fast enough… Submitting the answer form works for a grace period even after the closure. But really, I'd recommend you suggest an edit to the answers of the canonical topic if you think they should be updated – Bergi Apr 29 '20 at 18:58

1 Answers1

2

You can use Promise.allSettled(), the new Promise API would resolve when all the promise objects in the supplied array are settled (i.e. either fulfilled or rejected).

The value in the then callback would have an array of the objects having two keys status and value describing the result of each individual promise in the given array:

Promise.allSettled([
  Promise.resolve("Resolved Immediately"),
  new Promise((res, rej) => {
    setTimeout(res("Resolved after 3 secs"), 3000)
  }),
  Promise.reject(new Error("Rejected Immediately"))
]).then(arr => console.log(arr));
Fullstack Guy
  • 16,368
  • 3
  • 29
  • 44
  • Appropriate response in the modern JS ecosystem. Glad this made it through regardless of the closure of the question. – gelliott181 Apr 29 '20 at 18:46