0

I'm developing nodeJS server with Express. As you all know, network process should be processed asynchronously. And people usually deal with the result using callback function or Promise(async/await).

In my case, I need to send dozens of request to another server to get data. but I think requesting these one by one is inefficient. So I found Promise.all() and Promise.allSettled() which process several works asynchronously and get all results of works at once. And I also found worker_thread in nodeJS, which results in similarly.

I can not figure out which one is proper and what is difference between them. Does anybody help me?

6991httam
  • 305
  • 4
  • 14

2 Answers2

3

Since node is single-threaded, worker threads are meant to be used to prevent blocking the event loop, from a long and complex computational task that is time consuming. However, they are not good candidate for I/O intensive work.

From node documentation

Workers (threads) are useful for performing CPU-intensive JavaScript operations. They will not help much with I/O-intensive work. Node.js’s built-in asynchronous I/O operations are more efficient than Workers can be.

So i think in this case you should probably use Promise.all/allSettled

Sven.hig
  • 4,449
  • 2
  • 8
  • 18
1

Worker threads are for multi-threading cpu intensive stuff. Promise.all or Promise.allSettled are probably your best bets here. Which one you choose depends on how you want to deal with rejects/resolves.

This guy's answer will probably clear up the differences for you: https://stackoverflow.com/a/59784198/9916277

justin
  • 192
  • 1
  • 6