tiny-limit
v1.1.0
Published
Run async functions with limited concurrency. Same API as p-limit, ships ESM + CJS with zero dependencies.
Downloads
389
Maintainers
Readme
tiny-limit
Run async functions with limited concurrency. Same API as p-limit, but ships both ESM and CJS with zero dependencies.
import { pLimit } from "tiny-limit";
const limit = pLimit(5);
const results = await Promise.all(urls.map((url) => limit(() => fetch(url))));496 bytes gzipped. Zero dependencies.

Install
npm install tiny-limitUsage
import { pLimit } from "tiny-limit";
const limit = pLimit(3);
const input = [
limit(() => fetchUser(1)),
limit(() => fetchUser(2)),
limit(() => fetchUser(3)),
limit(() => fetchUser(4)),
limit(() => fetchUser(5)),
];
const users = await Promise.all(input);Pass arguments
const limit = pLimit(2);
const result = await limit(fetch, "https://example.com");Inspect the queue
limit.activeCount; // currently running
limit.pendingCount; // waiting in queue
limit.concurrency; // get or set the limit
limit.clearQueue(); // discard pending tasksDifferences from p-limit
p-limit v4+ is ESM-only. If you require("p-limit") in a CommonJS project, you get ERR_REQUIRE_ESM. tiny-limit works with both import and require().
| | p-limit | tiny-limit |
| ------------ | ---------------------- | ------------ |
| CJS support | v3 only (v4+ ESM-only) | ESM + CJS |
| Dependencies | yocto-queue | 0 |
| TypeScript | native (v6+) | native |
| Export | default | named |
Migrating from p-limit
- import pLimit from "p-limit";
+ import { pLimit } from "tiny-limit";One line. The rest of your code stays the same.
API
pLimit(concurrency: number): LimitFunction
Returns a limit function that queues async work up to the given concurrency. Throws if concurrency is not a positive integer (or Infinity).
limit(fn, ...args): Promise
Queues fn(...args) and returns a promise.
limit.activeCount
Number of promises currently running.
limit.pendingCount
Number of promises waiting in the queue.
limit.concurrency
Get or set the concurrency limit at runtime.
limit.clearQueue()
Discards all pending tasks.
