javascriptnode.jspromiseworker-thread

nodejs Worker_thread vs Promise.all or allSettled()


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?


Solution

  • 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