Implementing Promise.all in javascript

In javascript promise is used for doing asynchronous operations. Example: Making an api call or a timeout function.

Promise.all is used for making a list of promises and get the aggregated result.

Description

Promise.all will return a promise once all the promises are resolved. If any one of the promise is failed it will reject immediately with the error value of the promise that is rejected regardless of whether the remaining promises are resolved or not.

Let’s take an asyncOperation function which will resolves value after the given time.

var asyncOperation = function(time) {
return new Promise(function (resolve, reject) {
setTimeout(function() {
resolve(time);
}, time);
});
}

Now let’s use the Promise.all

var promisesToMake = [asyncOperation(2000), asyncOperation(1000), asyncOperation(3000)];
var promises = Promise.all(promisesToMake);
promises.then(function(results) {
console.log(results);
});

The output will be [2000, 1000, 3000]

Promise.all will aggregate the promise results in the same order as we’ve given

Now let’s implement the Promise.all by ourselves.

var promiseAll = function(promises) {
var results = [];
var completedPromises = 0;
return new Promise(function (resolve, reject) {
promises.forEach(function(promise, index) {
promise.then(function (value) {
results[index] = value;
completedPromises += 1;
if(completedPromises === promises.length) {
resolve(results);
}
}).catch(function (error) {
reject(error);
});
});
});
}

Here we’ll iterate through the list of promises and after each promise completes we’ll add the result to the results array at the same index as the promise, because the order will be preserved in the Promise.all Once all the promises are resolved we should return the array with the results.

If any one of the promise is rejected we should reject immediately.

Now if we call this function with a list of promises, it’ll produce the same result asPromise.all

var promisesToMake = [asyncOperation(2000), asyncOperation(1000), asyncOperation(3000)];
var promises = promiseAll(promisesToMake);
promises.then(function(results) {
console.log(results); // [2000, 1000, 3000]
});

Let’s modify the asyncOperation function, to check whether it is rejecting if any promise is failed.

var asyncOperation = function(time) {
return new Promise(function (resolve, reject) {
setTimeout(function() {
if(time === 2000) {
reject("error at 2000");
}

resolve(time);
}, time);
});
}

This will be rejected if time is equal to 2000, then promiseAll should be rejected immediately.

var promisesToMake = [asyncOperation(2000), asyncOperation(1000), asyncOperation(3000)];
var promises = promiseAll(promisesToMake);
promises.then(function(results) {
console.log(results);
}).catch(function(error) {
console.log(error);
});;

This will give the output as error at 2000 , because that’s the promise value that is rejected.

Like what you read? Give Murali Krishna a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.