最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Multithreading with javascript promises - Stack Overflow

programmeradmin5浏览0评论

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

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

Share Improve this question asked Dec 6, 2020 at 9:31 TheWommiesTheWommies 5,07212 gold badges65 silver badges83 bronze badges 2
  • What PromisePool are you talking about? – Bergi Commented Dec 6, 2020 at 11:43
  • Yes, the two then callbacks in your code will run synchronously after each other. – Bergi Commented Dec 6, 2020 at 11:43
Add a ment  | 

2 Answers 2

Reset to default 9

Javascript is single threaded right?

No. That's a mon 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.

Javascript is single threaded right?

Yes, one piece of JavaScript code always runs in one agent and one agent only can execute one function at a time.

So when it uses fetch api to make http requests it all happens in one thread?

No, not really. While your JavaScript code can not run in parallel to other JavaScript code, the browser can do other things in parallel (such as rendering the page, garbage collection, ...) including doing requests for you. Once the response es back, the result gets handed over back into JavaScript through resolving the promise.

How does it manage concurrency?

Whilst two functions cannot run concurrently, two async functions can (or Promise chains), because they are posed of multiple execution units, and once of them finishes the other one can take over:

 (async function first() {
    await undefined;
    console.log(2);
    await undefined;
    console.log(4);
 })();
 (async function second() {
    console.log(1);
    await undefined;
    console.log(3);
 })();

So JavaScript does have some form of concurrency, but on the level of functions (or blocks in an async function) and not on the level of single instructions.

To get real parallel execution, you need multiple agents (WebWorkers, ServiceWorkers), they can then also share memory in a limited way.

发布评论

评论列表(0)

  1. 暂无评论