Why onRejected is not called Promise.all () where Promise.reject () included in the array passed to Promise.all ()?

advertisements

Given

var promises = [Promise.resolve("a"), Promise.reject("b")];

Promise.all(promises.map(function(p, index) {
  return p.then(function(data) {
    console.log("inside .map()", data, "index", index)
    return data
  }, function(err) {
    console.log(err);
    return err
  })
}))
.then(function(complete) {
  console.log("all promises after .map()", complete)
}, function(err) {
  console.log("err", err)
})

why is onRejected not called at .then(onFulfilled, onRejected) following Promise.all() ?

jsfiddle https://jsfiddle.net/9gprLc7q/

https://jsfiddle.net/9gprLc7q/


What you've really done here is something like this:

https://jsfiddle.net/9gprLc7q/5/

var notRejectedPromise =
    Promise.reject("b")
      .then((resolved) => resolved, (err) => err)

var promises = [Promise.resolve("a"), notRejectedPromise];

Promise.all(promises)
.then(function(complete) {
  console.log("all promises after .map()", complete)
}, function(err) {
  console.log("err", err)
})

But deciding to handle the err portion by returning whatever err was, you returned a string. This is not a reason for rejection.

To actually cause the Promise.all() to reject you need an error to occur in either the resolved or rejected portion of .then

Given this, if you return a rejected promise, it will reject:

https://jsfiddle.net/9gprLc7q/3/

console.log(err)

to

return Promise.reject(err)

Alternatively you can throw an error: https://jsfiddle.net/9gprLc7q/2/

console.log(err)

to

throw new Error(err)