Skip to content

SoraKumo001/promise-parallels

Repository files navigation

@node-libraries/promise-limit

Limits the maximum number of asynchronous processes that can be executed.
The number of executions is controlled at the loop stage, so no memory is wasted in the queue.

usage

  • promiseLimit() to create an instance
  • add() to store Promise
  • wait() waits for a specified maximum number
  • all() waits for remaining processes
import { promiseLimit } from '@node-libraries/promise-limit';
const main = async () => {
  const ps = promiseLimit();
  for (let i = 0; i < 10; i++) {
    // Execute a random exit process and save the Promise
    ps.add(new Promise((resolve) => setTimeout(() => resolve(i), Math.random() * 100)));
    // Specify the maximum number of parallel execution and wait
    // Return value false: the process has not finished within the limit
    // Return index of the process returned by resolve
    const v = await ps.wait(5);
    console.log(`${i}:${v}`); // Number of loops: display the finished function
  }
  // wait for remaining processing if the loop exits with less than 5 in parallel
  (await ps.all()).forEach((v) => console.log(`*:${v}`));
};

main();

Execution result

0:false
1:false
2:false
3:false
4:3
5:1
6:2
7:4
8:0
9:6
*:5
*:7
*:8
*:9

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published