p-limiter
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

p-limiter

Zero dependency, lightweight concurrent Promise limiter

Install

yarn add p-limiter
# or
pnpm add p-limiter

Usage

import Limiter from 'p-limiter';

const limiter = new Limiter(3);

const mockPromise = (i: number) =>
  new Promise((resolve) => {
    console.log(
      `➕ Start Task${i} -`,
      `Active Count: ${limiter.activeCount}`,
      `Pending Count: ${limiter.pendingCount}`
    );
    setTimeout(() => {
      console.log(
        `✅ Complete Task${i} -`,
        `Active Count: ${limiter.activeCount}`,
        `Pending Count: ${limiter.pendingCount}`
      );
      resolve(i);
    }, i * 1000);
  });

(async () => {
  const results = await Promise.allSettled(
    [...new Array(6)].map((_, i) => limiter.limit(() => mockPromise(i)))
  );
  console.log('results: ', results);
})();

License

MIT License

Related

  • p-limit - Run multiple promise-returning & async functions with limited concurrency

Package Sidebar

Install

npm i p-limiter

Weekly Downloads

1,315

Version

1.0.0

License

MIT

Unpacked Size

5.92 kB

Total Files

6

Last publish

Collaborators

  • islizeqiang