Skip to content

ofershap/tiny-limit

Repository files navigation

tiny-limit

npm version npm downloads CI TypeScript License: MIT

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.

Demo

Demo built with remotion-readme-kit

Install

npm install tiny-limit

Usage

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 tasks

Differences 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.

The tiny-* family

Drop-in replacements for sindresorhus async utilities. All ship ESM + CJS with zero dependencies.

Package Replaces What it does
tiny-limit p-limit Concurrency limiter
tiny-map p-map Concurrent map with order
tiny-retry p-retry Retry with exponential backoff
tiny-queue p-queue Priority task queue
tiny-ms ms Parse/format durations
tiny-escape escape-string-regexp Escape regex chars

Want all async utilities in one import? Use tiny-pasync.

Author

Made by ofershap

LinkedIn GitHub


If this saved you from ERR_REQUIRE_ESM, star the repo or open an issue if something breaks.

License

MIT © Ofer Shapira

About

Modern p-limit replacement — async concurrency limiter. TypeScript, ESM + CJS, zero deps.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors