Inspiring

RIP hockey great Borje Salming, 71, who succumbed to Lou Gehrig’s Disease. Born in Sweden, he won two world championship medals playing for the national team. He did everything in his 23-year career…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




How to wait for multiple Promises?

Use Promise.all to wait for multiple promises in Node.js

In some use cases in Node.js it’s needed to execute multiple asynchronous operations concurrently and wait for them all to complete, because the combined result has to be processed. An example of this would be to wait for multiple API calls to finish before collecting all results and create a new combined API call. There are several ways to accomplish this. You can do this with the async/await and try/catch pattern or with thePromise.all() method. This blog article is about the latter one.

Promise.all waits for all fulfillments (or the first rejection).

The Promise.all() method takes an iterable of promises as an input (commonly an array), and returns a single Promise that resolves to an array of the results of the input promises.

This returned Promise will resolve when all the input promises have resolved, or if the input iterable contains no promises.

It rejects immediately upon any of the input promises rejecting or non-promises throwing an error, and will reject with this first rejection message / error.

The Promise.all method needs an iterable as an input, an array with promises and it will behave as a single Promise. So we can add a .then handler to handle the returned Promise, which will receive the result of the resolved Promises. To catch and handle potential errors a catch handler should be attached as well.

Let’s look at an example. We create 3 promises each with a different timeout. When all promises are resolved, it should output the combined response.

Please note that all inner Promises are started at the same time, so it takes 3 seconds instead of 6 seconds (1+2+3).

Let’s look at a more real example. We make a request for each element in an array. In the example we are going to request five todos based on their id from a placeholder API.

Create a project folder.

Initialize project with npm init -y to be able to install node packages.

Install node-fetch to make fetch requests.

Create an index.js file.

Add code.

If one Promise in the iterable object throws an error, all other Promises will be halted and if there have been already successfully made requests the results will not be returned. To still receive the result from Promise.all in a case where some Promises reject, we need to make the Promise.all utility fault tolerant.

To avoid losing the other responses, a catch handler can be attached to the individual Promises. This way we are catching the errors they might throw, instead of letting them bubble up to the Promise.all, which will cause the Promise to reject. The code could look something like this:

Add a comment

Related posts:

DottcoHomes Oak Grove Estates

Dottco Homes is pleased to announce a new subdivision at Oak Grove Estates, located in the Greater Marysville Area just outside of Edgewater with NO MELLO ROOS. This cozy, single story, cul-de-sac…

Best Features Of Traveling By Train

If you are planning a trip to Russia, it is sensible to start believing about what you will do there, and how you will pay for it. Of course, you can not just go to this nation with U.S. cash and…