Skip to content
Advertisement

Multithreading with javascript promises

Just learning promises. Javascript is single threaded right? So when it uses fetch api to make http requests it all happens in one thread?

How does it manage concurrency with PromisePool then?

var p = Promise(...)
p.then(
...//stuff1
)
p.then(
//stuff2
)

Then two then above cannot run on multiple threads right? Just in one thread? Thanks

Advertisement

Answer

Javascript is single threaded right?

No. That’s a common over-simplification.

JavaScript runs a main event loop, which can do only one thing at a time.

Generally all your JavaScript will run on that one event loop, so only one piece of JS will run at a time.

However, many JavaScript functions call code which isn’t JavaScript. Take fetch in a browser, for example. The responsibility for making the HTTP request is taken care of by the browser outside the main event loop so it can be making multiple requests and waiting for the responses while the JS program continues to run other tasks.

Web Workers (browsers) and Worker Threads (Node.js) are tools to let you move JS code outside the main event loop.

These can be implemented using threads.


I have some code which searches the file system for audio files, and then extracts the metadata from them. Once all the metadata is collected, it is passed on for further processing.

My current implementation uses a for loop with await so that only one file is being processed for metadata at once.

My first attempt tried to do them in parallel and attempting to read hundreds of audio files simultaneously used up all the RAM on my system.

I could switch to Promise Pool and read, for example, 4 files at a time (1 per CPU core) to get the best of both worlds.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement