Promise.all()


The Promise.all() method is used to process multiple Promises at the same time. The method takes in an array of Promises and then waits for them to all to resolve. Once they have all finished resolving, an array of results can be obtained by using the then() method. If any of the Promises reject, then the Promise.all() method will return the first rejected Promise.


Notice how the Promise.all() method is used to handle multiple Promises at the same time:

let promise1 = Promise.resolve('hello'); 
let promise2 = Promise.resolve({age:2,height:188}) 
let promise3 = 42; //normal values work with Promise.all() too

Promise.all([promise1,promise2,promise3])
    .then(result => { 

    console.log(result) //logs the array ["hello",{age:2,height:188},42]

    }).catch(error => {

        console.log(error) 

    });


Notice how Promise.all() method call rejects when one of the Promises that it is processing rejects:

let promise1 = Promise.resolve('hello'); 
let promise2 = Promise.resolve({age:2,height:188}) 
let promise3 = Promise.reject('failure.'); //rejected promise


Promise.all([promise1,promise2,promise3])
    .then(result => { 

        console.log(result) //doesn't occur since promise3 rejected

    }).catch(error =>

        console.log(error)  //logs 'failure.'

    );

results matching ""

    No results matching ""