Checking if Multiple Promises are Completed in Javascript

javascript
Published on August 10, 2019

Use-Cases of this Tutorial

  • Find whether multiple Promises have all finished their operations or not.

There are situations where an application needs to wait for the completion of multiple Promises and needs to know when they have all finished. In such cases Promise.all() or Promise.allSettled() methods can be used.

Checking if All Promises are Resolved Successfully

The Promise.all() method can be used to check whether all Promises have fulfilled successfully. It accepts an iterable object (e.g. an array) of multiple Promises and returns a Promise.

  • The returned Promise is resolved if all the input Promises passed to it are resolved. It is resolved with an array containing resolved values of all given Promises. Values in the array will be in order of the Promises passed, and not fulfillment order.
  • The returned Promise is rejected if any of the input Promises are rejected. It is rejected with the same reason as the first rejected Promise.
// all promises are resolved

var proA = new Promise((res, rej) => {
    setTimeout(() => {
        res("First");
    }, 2000);
});

var proB = new Promise((res, rej) => {
    setTimeout(() => {
        res("Second");
    }, 4000);
});

var proC = new Promise((res, rej) => {
    setTimeout(() => {
        res("Third");
    }, 6000);
});

Promise.all([proA, proB, proC]).then((values) => {
    // ["First", "Second", "Third"]
    console.log(values);
}).catch((reason) => {
    console.log(reason);
});
// all promises are not resolved

var proA = new Promise((res, rej) => {
    setTimeout(() => {
        res("First Success");
    }, 2000);
});

var proB = new Promise((res, rej) => {
    setTimeout(() => {
        res("Second Success");
    }, 4000);
});

var proC = new Promise((res, rej) => {
    setTimeout(() => {
        rej("Third Error");
    }, 6000);
});


Promise.all([proA, proB, proC]).then((values) => {
    console.log(values);
}).catch((reason) => {
    // "Third Error"
    console.log(reason);
});

Checking if All Promises are Settled (either Resolved or Rejected)

The problem in Promise.all() method is that even if a single Promise gets rejected, the values of resolved Promises are lost. Sometimes it may be the case that we just want to know whether all Promises have finished their operations or not — regardless they succeed or fail. This can be solved through Promise.allSettled().

The Promise.allSettled() method can be used to check whether all Promises passed to it have completed — either resolved or rejected. The return value is a Promise which will always be resolved, never rejected.

The returned Promise will be fulfilled with an array containing handlers of all the input Promises. Status of each Promise can then be found to check whether they were resolved or rejected.

// all promises are not fulfilled

var proA = new Promise((res, rej) => {
    setTimeout(() => {
        res("First Success");
    }, 2000);
});

var proB = new Promise((res, rej) => {
    setTimeout(()=>{
        res("Second Success");
    }, 2600);
});

var proC = new Promise((res, rej) => {
    setTimeout(()=>{
        rej("Third Error");
    }, 4000);
});

Promise.allSettled([proA, proB, proC]).then((results) => {
    // "Resolved : First Success"
    // "Resolved : Second Success"
    // "Rejected : Third Error"
    for(let i=0; i<results.length; i++) {
        if(results[i].status == 'fulfilled')
            console.log('Resolved : ' + results[i].value);
        else if(results[i].status == 'rejected')
            console.log('Rejected : ' + results[i].reason);
    }
});

Useful Resources

In this Tutorial