axios.all spread and catch all axios.all spread and catch all ajax ajax

axios.all spread and catch all


The problem (as you already know) is that you will get into catch block as soon as the first promise rejects, making it impossible to collect all failed responses in the same catch. However, you still can handle failed promises manually to aggregate errors and throw afterwards.

Check it this will work for you:

const promises = [  axios.get('http://some_url'),  axios.get('http://another_url'),]const promisesResolved = promises.map(promise => promise.catch(error => ({ error })))function checkFailed (then) {  return function (responses) {    const someFailed = responses.some(response => response.error)    if (someFailed) {      throw responses    }    return then(responses)  }}axios.all(promisesResolved)  .then(checkFailed(([someUrl, anotherUrl]) => {    console.log('SUCCESS', someUrl, anotherUrl)  }))  .catch((err) => {    console.log('FAIL', err)  });

You will get into catch block if at least one of the promises fails. You can find one which one by checking err array of responses.


I don't think this is possible due to the fail fast behaviour of Promise.all. If any of your requests fail, they will automatically be the culprit and the result in the catch.

    Promise.all([      Promise.reject(Error('1')),      Promise.reject(Error('2')),      Promise.reject(Error('3'))    ]).then((results) => {      console.log(results)    }, (error) => {      console.log(error.message)    })

This resulting code will always print 1 as it is the first to fail.I think a similar feature was requested on the repo and they said it wasn't possible.

I was going to leave this as a comment but don't have a high enough reputation yet.