If I understand correctly, you want to run the function if and only if all the promises are rejected, and you want something as elegant as it is Promise.all()
right ..? Then you can use Promise.all()
, but you need a little helper function for invert
promises.
var invert = pr => pr.then(v => Promise.reject(v), x => Promise.resolve(x));
Now your three deviation promises will do what you want.
var promises = [new Promise((resolve, reject) => {setTimeout(reject, 100, "1st promise rejected")}),
new Promise((resolve, reject) => {setTimeout(reject, 100, "2nd promise rejected")}),
new Promise((resolve, reject) => {setTimeout(reject, 100, "3rd promise rejected")})],
invert = pr => pr.then(v => Promise.reject(v), x => Promise.resolve(x));
Promise.all(promises.map(invert))
.then(errs => errs.forEach(err => console.log(err)));
Run codeHide resultIf any of your promises is resolved in some way, you can catch the resolution at the stage catch
if you want.
source
share