What types does the main pLimit function expect for its concurrency argument?
What types does the main pLimit function expect for its concurrency argument?
About sindresorhus/p-limit · 2 messages · last activity 7/20/2026
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
Optionstype, which requires aconcurrency: numberproperty (minimum1) and an optionalrejectOnClear?: booleanproperty (defaultfalse) [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
concurrencysetter validate new values dynamically? - What happens when
rejectOnClearis 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-limitPublic 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.