Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

JavaScript Promises. Part 4 — Promise.all, Promise.any and Promise.race

--

Up until this point, we have learned

  1. Why promises were introduced?
  2. Promise fundamentals. How to create and consume a promise?
  3. Promise chaining

Now we will look into some of the other methods in Promise API and when to use them.

Promise.all

The Promise.all() method takes an iterable of promises as the input, and returns a single Promise

This returned promise will only resolve when all of the input promises have resolved and its value will be an array of the results of the input promises.

If any of the input promises reject or throws an error, the returned promise will immediately reject with that error/message.

In the above example, we have 3 variables passed into the Promise.all method.

  1. Promise that will resolve immediately with value 3
  2. This is not a promise. which means we can also pass non-promise values into the Promise.all method.
  3. Promise that will resolve after 1 second with the value foo .

If you execute the above code, you will get a response — [3, 42, "foo"] after 1 second.

Why 1 second?

Because Promise.all will only resolve once all the input promises resolve. Here, the 3rd promise (using setTimeout) takes 1 second to resolve. And it is the last resolved promise. So Promise.all resolves after 1s.

As you can understand from the example, the response is an array of the results of the input promises.

Promise.all is used when you have multiple asynchronous operations that needs to be run in parallel, and once ALL of those operations are finished, you want to perform some operations.

Promise.any

Promise.any can be considered the functional opposite of Promise.all .

This too takes an iterable of promises as the input, and returns a single Promise

The returned promise will resolve as soon as one of the input promises fulfills (resolves).

The returned promise WILL NOT REJECT if any of the input promises reject.

However, if none of the input promises resolves, the returned promise will reject with an AggregateError

In the example, we have 3 promises

  1. Promise that will reject immediately with value 3
  2. Promise that will resolve after 2 seconds with value ‘resolved second
  3. Promise that will resolve after 1 second with value ‘resolved first

When you execute the above code, you will get the output ‘resolved first’ after 1 second (the first resolved promise), even though the first input promise rejects immediately.

Promise.any is used when you have multiple asynchronous operations that needs to be run in parallel, and you have to perform some operation once ANY of the input promises resolve, irrespective of the status of other input promises.

Promise.race

Promise.race is similar to Promise.any.

Unlike Promise.any which waits for any of the input promises to resolve, this method resolves/rejects as soon as one of the input promise resolves/rejects.

A race of promises if you will, where the first to either resolve/reject is the winner.

As you can understand from the example, the output will be — second Promise, since it resolves faster than the first promise.

Change the resolve to reject in the second promise, and the catch block will be executing.

Promise.race is used when you have multiple asynchronous operations that needs to be run in parallel, and you need to perform operations as soon as any one of the inputs resolves/rejects.

Summary

In this article, we learned about the different promise APIs available. Specifically, Promise.all, Promise.any and Promise.race.

To read the final article of the series, regarding async/await, click here.

If you have found this article helpful, make sure to clap and share it with your friends. If you have any suggestions/comments let me know. Make sure to follow me so you don’t miss any articles.

--

--

Nerd For Tech
Nerd For Tech

Published in Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Abdu Manaz C A
Abdu Manaz C A

Written by Abdu Manaz C A

Loves JS, java and anything tech.

No responses yet