author avatar

amber.srivastava

Mon Nov 04 2024

Promise.allSettled() :

Purpose: Executes multiple promises and waits for all of them to settle (either resolve or reject).
Returns: A promise that resolves with an array of objects. Each object has:
status: Either "fulfilled" or "rejected".
value: The resolved value (if fulfilled) or reason: The rejection reason (if rejected).
Example


const promises = [
  Promise.resolve(1),
  Promise.reject('Error'),
  Promise.resolve(2),
];

Promise.allSettled(promises).then((results) => {
  results.forEach((result) => {
    if (result.status === 'fulfilled') {
      console.log('Result:', result.value);
    } else {
      console.log('Error:', result.reason);
    }
  });
});

Output :
Result: 1
Error: Error
Result: 2


#CCT1JMA0Z