1

There is a nodejs module that lets you limit the number of concurrent promises:

https://github.com/sindresorhus/p-queue

Does this make use of multiple threads?

Example from the official page:

const {default: PQueue} = require('p-queue');
const got = require('got');

const queue = new PQueue({concurrency: os.cpus().length});

(async () => {
    await queue.add(() => got('sindresorhus.com'));
    console.log('Done: sindresorhus.com');
})();

(async () => {
    await queue.add(() => got('ava.li'));
    console.log('Done: ava.li');
})();

(async () => {
    const task = await getUnicornTask();
    await queue.add(task);
    console.log('Done: Unicorn task');
})();

Will it run each of the async functions above in different cpu threads?

Alex
  • 66,732
  • 177
  • 439
  • 641
  • Just curious - since when it became possible to do multi-threading in JavaScript? – Pa Ye Nov 16 '19 at 20:02
  • idk.. I am trying to find a way to use multiple threads, but everything I found so far requires some kind of communication between processes. It seems there's no way to do this easily from the same script, for having shared variables and so on... It's almost 2020, cpus have like 20 cores and we still can't use them all – Alex Nov 16 '19 at 20:05
  • i would recommend reading the difference between async and threads here: https://stackoverflow.com/questions/34680985/what-is-the-difference-between-asynchronous-programming-and-multithreading – fYre Nov 16 '19 at 20:09
  • @Alex "*I am trying to find a way to use multiple threads*" - why? What is your goal? – Bergi Nov 16 '19 at 20:31
  • to make my app use all cpu cores – Alex Nov 16 '19 at 20:37
  • See https://nodejs.org/api/cluster.html and https://nodejs.org/api/worker_threads.html for that. Promises might help you by letting you deal with asynchronous results from those threads easily, but they don't make anything parallel on their own. – Bergi Nov 16 '19 at 20:40

1 Answers1

4

Will it run each of the async functions above in different cpu threads?

No, of course not. That's not what async functions do. All they do is provide nicer syntax for sequential asynchronous code. They're still normal promises with promise callbacks under the hood, running on the same single-threaded event loop as always. See also Is promise.all useful given that javascript is executed in a single thread?

Of course, the got package would be free to use nodejs' worker thread features or something else to do its job, all it needs to do to play nicely with await is return a promise. But that won't affect your asynchronous IIFEs or the p-queue on which the functions are scheduled.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375