Mr. Jo
Mr. Jo

Reputation: 5261

Check if one promise failed using Promise.all() function

I'm new in promises and I'm currently trying to understand it. As I knew, Promise.all() has no function to check if one of the promises are rejected. So what I've tried is:

if ( Promise.all( promises ) ) {
    console.log( "Everything is resolves!" );
} else {
    console.log( "There is a rejected one!" )
}

But this seems to be not working. Is there any simple way to check this? I've found some questions with maps and tried it out but it looks wrong for my implementation.

Upvotes: 0

Views: 1769

Answers (2)

CertainPerformance
CertainPerformance

Reputation: 370739

Call .catch on the Promise call to see if at least one Promise rejects:

Promise.all(promises)
  .then((results) => {
    // ...
  })
  .catch((err) => {
    // At least one of the Promises rejected (or an error was thrown inside the `.then`)
  });

Upvotes: 2

Celso Wellington
Celso Wellington

Reputation: 889

Promises.all( [promises array] ).then(function).catch(errorfunction)

you can do like this:

Promise.all([promises array])
.then((promisevalue) => console.log('returned'))
.catch(error => console.log(error.message));

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all

Upvotes: 1

Related Questions