RepoPilot

What types does the main pLimit function expect for its concurrency argument?

About sindresorhus/p-limit · 2 messages · last activity 7/20/2026

Question

What types does the main pLimit function expect for its concurrency argument?

RepoPilot

pLimit's concurrency parameter accepts either a number or an Options object:

  • Type signature: pLimit(concurrency: number | Options): LimitFunction [index.d.ts:L89-L90]
  • If an object is passed, it must conform to the Options type, which requires a concurrency: number property (minimum 1) and an optional rejectOnClear?: boolean property (default false) [index.d.ts:L91-L106]

At runtime, [index.js:L3-L10] handles both forms:

export default function pLimit(concurrency) {
	let rejectOnClear = false;

	if (typeof concurrency === 'object') {
		({concurrency, rejectOnClear = false} = concurrency);
	}

	validateConcurrency(concurrency);
	...

If concurrency is an object, it destructures concurrency and rejectOnClear from it; otherwise it treats the argument directly as the numeric limit.

The actual validation happens in validateConcurrency [index.js:L123-L127]:

function validateConcurrency(concurrency) {
	if (!((Number.isInteger(concurrency) || concurrency === Number.POSITIVE_INFINITY) && concurrency > 0)) {
		throw new TypeError('Expected `concurrency` to be a number from 1 and up');
	}
}

So the effectively valid values for the resolved concurrency are:

  • A positive integer (≥ 1), or
  • Number.POSITIVE_INFINITY

Anything else — including non-integers like 1.2, non-numbers like undefined/true, 0, negative numbers, or an object missing a valid concurrency field — throws a TypeError. This is also confirmed by the test cases in [test.js:L275-L296], which check that pLimit(0), pLimit(-1), pLimit(1.2), pLimit(undefined), pLimit(true), and pLimit({}) all throw.

There's also an additional runtime check that rejectOnClear, if provided, must be a boolean, otherwise a TypeError is thrown too [index.js:L12-L14].

FOLLOW_UPS:

  • How does concurrency setter validate new values dynamically?
  • What happens when rejectOnClear is true and queue is cleared?
  • Why is this project flagged as unmaintained by OpenSSF Scorecard?

Want to ask your own question?

Open chat for sindresorhus/p-limit

Public Q&A. Generated by RepoPilot from the actual source of sindresorhus/p-limit. AI answers can be incomplete or stale — verify before relying on them.