I am trying to run several promises where there is a potential that one might fail. In the case that one does fail, how do I find out which one failed and still be able to access the result of promises that did not fail?
Right now, I am using Promise.all
, but if any of the promises fail, Promise.all
automatically goes into the catch error block so I can't access anything from my .then
block so I can't access any of the successful promise results. Can someone help?
My fiddle: https://jsfiddle.net/9u2nL7zj/
My code:
let promise1 = new Promise((resolve, reject) => {
console.log('-uno-')
resolve('-promise 1 success')
})
let promise2 = new Promise((resolve, reject) => {
console.log('-dos-')
reject('-promise 2 fail')
})
let promise3 = new Promise((resolve, reject) => {
console.log('-tres-')
reject('-promise 3 fail')
})
let promise4 = new Promise((resolve, reject) => {
setTimeout(() => {
console.log('-cuatro-')
resolve('-promise 4 success-')
}, 3000)
})
Promise.all([promise1, promise2, promise3, promise4]).then((res) => {
console.log('--done--', res)
}).catch((err) => {
console.log('--err--', err)
})