Exercise on Promises

Promise.all

const data = Promise.race([promise1,...]) // iterable object

You can extract promise resolved values using either .then syntax:-

data.then((values) => {
values; // [resultOfPromise1, resultOfPromise2, ...]
}).catch(error => {
console.log(error)
}

or async/await:

try{
const result = await data;
console.log(result); //[resultPromise1, resultPromise2, ...]
} catch (error => {
console.log(error);
}

It accepts an Iterable object such as an Array of promises as an input.

  • Will get the result if all promises get resolved.
  • Will get rejected if any of the promises get rejected.
  • Often used when there are multiple asynchronous tasks involved.
  • This method aggregates the result of multiple promises.

Example 1:-

As you can see, the result of promises is in the same order as in its source promises. Even though the first promise takes the longest time to resolve, it’s still first in the array of results.

Example 2:-

Here the second promise rejects in two seconds, which leads to immediate rejection of promise.all(), so .catch executes. Even one fails, the other will still continue to execute but their result will be ignored.

Read @ https://javascript.plainenglish.io/javascript-promises-understanding-promise-all-c6a126d02e86 for more.

Promise.race

Promise.race() Example

As you can see in the above example, the second promise is rejected instead of immediately rejecting the whole, just like the promise.all(), or instead of ignoring the rejected promise, promise.race() will return the fastest executed promise.

What makes Promise.race() different from Promise.any()

Promise.race and Promise.any do different things:

  • Promise.any() also returns the fastest promise but it returns the fastest fulfilled promise. And returns an aggregated error if all are rejected.
  • While Promise.race() returns the fastest settled promise, doesn’t matter whether any of the promises is rejected or resolved. And if all promises are rejected it will still return the fastest rejected promise.

Example:-

If you pass them both an array of two promises, and one of the promises is rejected, then afterward the other promise is fulfilled, the promise from Promise.race will be rejected (because the first promise to settle was rejected) and the promise from Promise.any will be fulfilled (because although the first promise was rejected, the second was fulfilled).

Promise.any Examle

In this example, both the promises get rejected with an error of ‘Ouch’ and ‘Error’ string. Therefore, promise.any() method will return an aggregated object that has these errors properties.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Anil Kumar

Anil Kumar

146 Followers

Experience with Front-end Technologies and MERN / MEAN Stack. Working on all Major UI Frameworks like React, Angular.