Could you elaborate why you think you should process something in a different thread?
Please check my detailed reply here regarding a similar question: How to truly make a time consuming request async in node
To summarize:
- Node.JS is executing your javascript code in one thread only (mainthread)
- Built-in modules like
fs
, http
, net
etc. make use of libuv
worker threads to process I/O in a background thread
- Thirdparty modules might do the same, it depends on their implementation
- If you want your code to be executed in a background thread, you have 2 options:
- Write a C-wrapper module which uses
libuv
worker threads to do the stuff
- Use
WorkerThreads
in your javascript code which were introduced in Node.JS 10 (Docs), but please note that the status of this module is experimental and not ready for production
Generally speaking, I highly doubt you want to use threads on your own. Your question indicates you want to download a file from a webserver. The available modules (fs
, request
, http
, ...) already make use of background threads to do the I/O, so it is unclear to me where on top of that you would need background processing.
Please clarify more, or think about sticking to the available modules in case you don't need "more multithreading".