I would like to execute multiple http requests in my Node.js app. I also want to wait until all of them have completed before proceeding to use the results. Is Javascript Promises the way this should be done or is there something more specific to Node that handles this better?
Asked
Active
Viewed 655 times
0
-
`Promise.all()` or `Promise.allSettled()` used with an http request library that uses promises (such as [got()](https://www.npmjs.com/package/got)) is the preferred method. – jfriend00 Sep 07 '20 at 08:56
2 Answers
1
Promise.all waits for all fulfillments (or the first rejection). You can go through below link for different ways to achieve this using Promise.allSettled().
Already been asked: Wait until all promises complete even if some rejected
or https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled

Dolly
- 2,213
- 16
- 38
-
It isn't clear whether Promises.all (or .allSettled) is running true asynchronous threading or this is being faked to make it look like it is. Javascript is a single threaded environment. Do Promises actually use multiple threads to get around this? – Johann Sep 07 '20 at 09:05
-
1Read a very useful post https://stackoverflow.com/questions/22844441/is-promise-all-useful-given-that-javascript-is-executed-in-a-single-thread#:~:text=Even%20though%20JavaScript%20runs%20in,in%20turn%20resolves%20the%20promises – Dolly Sep 07 '20 at 09:08
0
https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Promise/all
Promises.all() seems to be what you are looking for

Bamak
- 188
- 6
-
Do you know if multiple Promises (Promises.all) are actually running asynchronously, or are they being run synchronously? Javascript is really only a single threaded environment. – Johann Sep 07 '20 at 09:00
-
It is asynchronous. If you don't write your something outside of .then(), .catch() or .finally(), it won't wait for the end of the request. – Bamak Sep 07 '20 at 13:55