Introduction to Promise APIs

Introduction to Promise APIs

A prerequisite to understanding Promise APIs is to know what Promises are and how they work. If you are not familiar with Promises, you are advised to go through the documentation for Promises.

The Promise class in JavaScript has 6 static methods that we call 'Promise APIs'. Let's take a look at them one by one.

  • Promise.resolve/reject

Syntax:

Promise.resolve("Resolved");
Promise.reject("Error");

Promise.resolve is used to indicate that the asynchronous operation was successful. On the contrary, Promise.reject is used to indicate that there has been an error during the asynchronous operation.

In the below example, we can see the different ways of resolving/rejecting a Promise.

let promise1 = Promise.resolve("Hello World!");
let promise2 = new Promise((resolve, reject) => {
  resolve("Resolved");
});
let promise3 = Promise.reject("Rejected");
let promise4 = new Promise((resolve, reject) => {
  reject("Rejected");
});
  • Promise.all

Syntax:

Promise.all(iterable)

Promise.all resolves only when all of the Promises passed in the iterable are resolved.

In the below example, all the Promises passed in the iterable are resolved. In this case, Promise.all waits until all of them are resolved and then returns the list of values returned by each Promise.

let promise1 = Promise.resolve("Hello World!");
let promise2;
let promise3 = new Promise((resolve, reject) => {
  resolve("Resolved");
});
Promise.all([promise1,promise2,promise3]).then(values => console.log(values));

In the below example, one of the Promises passed in the iterable is not resolved. In this case, Promise.all rejects with that error and does not execute the other Promises in the iterable. Hence, the outcome of the rejected Promise becomes the outcome of the Promise.all method.

let promise1 = Promise.resolve("Hello World!");
let promise2 = new Promise((resolve, reject) => {
  reject("Rejected");
});
let promise3 = new Promise((resolve, reject) => {
  resolve("Resolved");
});
Promise.all([promise1,promise2,promise3]).then(values => console.log(values));
  • Promise.allSettled

Syntax:

Promise.allSettled(iterable)

If any one Promise is rejected in the case of Promise.all, the outcome of the Promise is rejected and none of the following Promises are executed. If we want to counter this behavior and would like to execute all the Promises in the iterable irrespective of the outcome, that is where Promise.allSettled works it's magic.

This method returns {status: "fulfilled", value: result} for successful responses and {status: "rejected", reason: error} for errors.

In the below example, even if the second Promise is rejected, the method returns an array of objects accordingly as mapped in the above paragraph instead of rejecting the Promise as a whole, which is what would have happened in the case of Promise.all.

let promise1 = Promise.resolve("Hello World!");
let promise2 = new Promise((resolve, reject) => {
  reject("Rejected");
});
let promise3 = new Promise((resolve, reject) => {
  resolve("Resolved");
});
Promise.all([promise1,promise2,promise3]).then(values => console.log(values));
  • Promise.race

Syntax:

Promise.race(iterable)

Promise.race looks for the first settled Promise and gets it's result in case the Promise is fulfilled or the error in case it is rejected.

In the below example, the outcome of Promise.race will be resolved since the first Promise that is settled is resolved and is returning a response.

let promise1 = Promise.resolve("Hello World!");
let promise2;
let promise3 = new Promise((resolve, reject) => {
  resolve("Resolved");
});

However, in the below example, the outcome of Promise.race will be rejected since the first settled Promise is rejected.

let promise1 = Promise.reject("Rejected");
let promise2;
let promise3 = new Promise((resolve, reject) => {
  resolve("Resolved");
});
  • Promise.any

Syntax:

Promise.any(iterable)

Similar to Promise.race but instead of looking for the first settled Promise, this method looks for the first fulfilled Promise.

If all the Promises are rejected, it returns a special AggregateError that stores all the errors in it's errors property.

In the below example, the Promise.any returns 'Resolved' since it's the first fulfilled Promise.

let promise1 = Promise.resolve("Resolved");
let promise2;
let promise3 = new Promise((resolve, reject) => {
  resolve("Resolved");
});

The below example throws the AggregateError that we spoke about earlier.

let promise1 = Promise.reject("Rejected");
let promise2 = new Promise((resolve, reject) => {
  reject("Rejected");
});

The error can be accessed through a catch block and the list of errors can be accessed through the errors property of the error.