Learn About Promise API

ยท

3 min read

Learn About Promise API

There are 6 static methods in the Promise class. Let's discuss them one by one.

Promise.resolve

Promise.resolve(value) creates a resolved promise with the result value.

     const promise = new Promise(resolve => resolve('value')); // value

Promise.resolve to make a promise of it, so the returned value is always a promise.

Promise.reject

Promise.reject(error) creates a rejected promise with error.

     const promise = new Promise((resolve,reject) => reject('error')); // error

In practice, this method is almost never used.

Promise.all

Let's say we want many promises to execute in parallel and wait until all of them are ready. For instance, we want to call multiple APIs in parallel, in case Promise.all is used.

The syntax is:

     const promise1 = Promise.resolve('promise1');
     const promise2 = Promise.resolve('promise2');
     const promise3 = Promise.resolve('promise3');
     Promise.all([promise1,promise2,promise3]).
     then(data => console.log(data)).catch(err => console.(err)); // ['promise1','promise2','promise3']

If any of the promises is rejected, the promise returned by Promise.all immediately rejects with that error.

The syntax is:

     const promise1 = Promise.resolve('promise1');
     const promise2 = Promise.reject('promise2');
     const promise3 = Promise.resolve('promise3');
     Promise.all([promise1,promise2,promise3]).
     then(data => console.log(data)).catch(err => consoel.log(err)); // promise2

Here the second promise is rejected. That leads to an immediate rejection of Promise.all, so .catch executes: the rejection error becomes the outcome of the entire Promise.all.

Promise.allSettled

Promise.allSettled just waits for all promises to settle, regardless of the result. The resulting array has:

  • {status:"fulfilled",value:result} for successful responses.
  • {status:"rejected", value:error} for errors.

The syntax is:

     const promise1 = Promise.resolve('promise1');
     const promise2 = Promise.reject('promise2');
     const promise3 = Promise.resolve('promise3');
     Promise.all([promise1,promise2,promise3]).
     then(data => console.log(data)); 
     /* 0: {status: 'fulfilled', value: 'promise1'}
         1: {status: 'rejected', reason: 'promise2'}
         2: {status: 'fulfilled', value: 'promise3'}
     */

Now we can use Promise.allSettled to get the results of all given promises, even if some of them reject.

Promise.race

Similar to Promise.all, but waits only for the first settled promise and gets its result (or error).

The syntax is:

     const promise1 = new Promise((resolve,reject) => setTimeout(() => 
     resolve('promise1'),2000));
     const promise2 = new Promise((resolve,reject) => setTimeout(() => 
     reject('promise2'),1000));
     const promise3 = new Promise((resolve,reject) => setTimeout(() => 
     resolve('promise3'),3000));
     Promise.race([promise1,promise2,promise3]).then(data => console.log(data)) //promise2

The first promise here was the fastest, so it became the result. After the first settled promise "wins the race", all further results/errors are ignored.

Promise.any

Similar to Promise.race, but waits only for the first fulfilled promise and gets its result.

The syntax is:

     const promise1 = new Promise((resolve,reject) => setTimeout(() => 
     resolve('promise1'),2000));
     const promise2 = new Promise((resolve,reject) => setTimeout(() => 
     reject('promise2'),1000));
     const promise3 = new Promise((resolve,reject) => setTimeout(() => 
     resolve('promise3'),3000));
     Promise.race([promise1,promise2,promise3]).then(data => console.log(data)) //promise1

The second here was the fastest, but it was rejected promise, so the next fastest promise is the first promise that became the result. After the first fulfilled promise "wins the race", all further results are ignored.

  • If all of the given promises are rejected, then the returned promise is rejected with AggregateError - a special error object that stores all promise errors in its errors property.

Summary

  • Promise.resolve(value) - makes a resolved promise with the given value.
  • Promise.reject(error) - makes a rejected promise with the given value.
  • Promise.all(promises) - waits for all promises to resolve and returns an array of their results. If any of the given promises rejects, it prints a reject error and all other further promises are ignored.
  • Promise.allSettled(promises) - waits for all promises to settle and returns their results as an array, regardless of the result.
  • Promise.race(promises) - waits for the first promise to settle, and it's result/error becomes the outcome.
  • Promise.any(promises) - waits for the first promise to be fulfilled, and its result becomes the outcome. If all of the promises are rejected, AggregateError becomes the outcome.

Thanks for read this article ๐Ÿ˜† ๐Ÿ˜† ๐Ÿ‘

ย