An Introduction To Promise.all Function

Posted By : Manisha Kirodiwal | 25-Apr-2018

The Promise.all(iterables) function returns a single Promise.Here we provide multiple Promises as argument. Promise.all(iterables) function returns promise only when all the promises (argument) have resolved.
It rejects when first promise argument reject.

var promise1 = Promise.resolve(3);
var promise2 = 42;
var promise3 = new Promise(function(resolve, reject) {
  setTimeout(resolve, 100, 'foo');
});

Promise.all([promise1, promise2, promise3]).then(function(values) {
  console.log(values);
// expected output: Array [3, 42, "foo"]
});

Syntax:

Promise.all(func1, func2 [,funcN])

Parameters:

func1
Required. A function that returns a promise.
func2
Required. A function that returns a promise.
funcN
Optional. One or more functions that return a promise.

 

Return Value:

  • A promise already resolved if the iterable pass is empty.
  • A promise resolved asynchronously if the last iteration contains no promises. Keep in mind that Google Chrome 58 returns a promise already resolved in this case.
  • A promise pending in all other cases. This promise returned is resolved / rejected asynchronously (as soon as the stack is empty) when all the promises in the given iterable have been resolved, or if any of the promises rejects. See the example on "Asynchronicity or synchronicity of Promise.all" below. The returned values will be in order of the approved promises, regardless of the order of completion.

Description:

This method can be useful for adding the results of multiple promises.

 

Fulfillment:
If an empty iteration is passed, this method returns (synchronously) a promise already resolved.
If all promises approved meet, or
are not promises, the promise returned by Promise.all is fulfilled asynchronously.
In all cases, the promise returned is fulfilled with a matrix containing all the values of the iterable past as an argument (also non-promising values).

 

Rejection:
If any of the approved promises are rejected, Promise.all rejects asynchronously the value of the rejected promise, regardless of whether the other promises have been resolved or not.

About Author

Author Image
Manisha Kirodiwal

Manisha is a Web Application developer and she is good in working with Angular, TypeScript , JavaScript and Jquery. In free time she loves to dance and cooking.

Request for Proposal

Name is required

Comment is required

Sending message..