Promise allSettled introduction

promises are the implementation of asynchronous operations. Promises in javascript are used to handle asynchronous operations. Each promise has three states

  • fulfilled or resolve - for successful operations
  • rejected - for failure operations
  • neither fulfilled nor rejected

Before ES6 implementation, We have promises implemented in libraries like bluebird and Q. With ES6 or ES2015, Promises are implemented inbuilt standard part of the javascript core language. all(), race, reject, and resolve methods are implemented with the Promise class in ES6.

allSettled() method introduced in Promise class with ES2020 to javascript feature.

Let us see the following proimse.all() example before ES2020.

Created three promises - proimse1 will be resolved. proimse2 will be rejected. proimse3 will be resolved.

promise.all() takes an array of promises, halts its execution, throws an error, and rejects the promise. once rejected promise occurred in an array of promises.

const proimse1 = new Promise((resolve, reject) => setTimeout(resolve, 500));
const proimse2 = new Promise((resolve, reject) => setTimeout(reject, 1000));
const proimse3 = new Promise((resolve, reject) => setTimeout(resolve, 2000));

Promise.all([proimse1, proimse2, proimse3])
    .then((values) => {
        console.log('values= '+values)
        })
    .catch((error)=>{
        console.log('error='+error);
        });

Output is

Promise { <pending> }
error=undefined

The first promise is resolved and printed its Promise is its value, the second promise is rejected, and the third promise is not executed.

Promise.allSettled() returns the result of all promises results, irrespective of rejected or fulfilled from an array of promises.

Syntax:

Promise.allSettled([array of promises])

The return type and input parameters

  • Input is an array of promises
  • Returned type is array of promises with each promises result is { status : ‘fulfilled/rejected’, value: “userdefined” }

Let’s see an example.

const proimse1 = new Promise((resolve, reject) => setTimeout(resolve, 500));
const proimse2 = new Promise((resolve, reject) => setTimeout(reject, 1000));
const proimse3 = new Promise((resolve, reject) => setTimeout(resolve, 2000));

Promise.allSettle([proimse1, proimse2, proimse3])
.then((values) => {console.log('values= '+values)})
.catch((error)=>{
  console.log('error='+error);
});

And the output is

[{ status : 'fulfilled', value: "undefined" },
{ status : 'rejected', value: "error" },
{ status : 'fulfilled', value: "undefined" }]

Difference between allSettled and all method in a promises

allSettledall
input is an array of promisesinput is an array of promises
Resolves all promises even though one of the promise is rejectedReject promise if one of the promises is rejected
Result array length is equal to input lengthResult array length is less than equal to input length
Useful for getting status of multiple API -sucess/failure operationsUseful for multiple calls for success operations