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


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 onrejected not called @ .then(onfulfilled, onrejected) following promise.all() ?

jsfiddle https://jsfiddle.net/9gprlc7q/

https://jsfiddle.net/9gprlc7q/

what you've done here 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 handle err portion returning whatever err was, returned string. not reason rejection.

to cause promise.all() reject need error occur in either resolved or rejected portion of .then

given this, if return rejected promise, reject:

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

console.log(err) 

to

return promise.reject(err) 

alternatively can throw error: https://jsfiddle.net/9gprlc7q/2/

console.log(err) 

to

throw new error(err) 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -