Promise.all() in javascript.

You might have heard or used Promise.resolve() and Promise.reject() all the time but there is one more method of Promise that you may have rarely used. That is Promise.all() .We will see how to use this method  of Promise and few examples related to this method.

Promise.all() :

This method accepts iterable object of promises and resolves all the promises. The returned promise has an array containing all the values of the resolved promises that is passed.

If any of the iterable promise is rejected then the Promise.all() rejects otherwise it fulfills all the promises and returns an array.

Lets look at few examples of Promise.all() and understand it better.

Example 1 :

var promise1 = Promise.resolve('a');
var promise2 = Promise.resolve('b');
var resolvedPromise = Promise.all([promise1,promise2]);
resolvedPromise.then((data) => {
  console.log(data); // This prints ['a','b'];
});

The above example is one way of using the Promise.all(). It takes array of 2 promises promise1 and promise2 and resolves both and returns a new promise. If you print the data returned by the Promise.all() you will see an array which contains the values resolved by promise1 and promise2. In the above example values ['a','b'] is printed.

Example 2 :

var promise1 = Promise.resolve('a');
var promise2 = Promise.reject('b');
var resolvedPromise = Promise.all([promise1,promise2]);
resolvedPromise.then((data) => {
  console.log(data);  // It never gets resolved and hence this is not printed
}).catch((error) => {
  console.log(error); // Prints value b. Which is rejected by promise2
});

In the above example Promise.all() took an array of promises where one resolves and other rejects.  Promise.all() always rejects if one of the promise that is passed gets rejected.

You need to be careful while using Promise.all(). It needs to be used in a context where you need to show an error if any of the promise rejects. This is not the case always during application development. You don want to see error everywhere in the page just because one of the promise got rejected.

Example 3 :

var promise1 = Promise.resolve('a');
var resolvedPromise = Promise.all([promise1,'b']); // Passing a non promise value
resolvedPromise.then((data) => {
  console.log(data);  // Prints a,b
})

In the above example we passed the non promise value to Promise.all(). We just passed character 'b' to the Promise.all() function. The value will be just added to the resolved promise and still be present in the array. Usually the value is ignored and its not evaluated but its returned in the Promise rather than omitting it.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s