Skip to content

Commit

Permalink
Fix check for invalid concurrency argument
Browse files Browse the repository at this point in the history
Fixes #15
  • Loading branch information
sindresorhus committed Aug 18, 2019
1 parent 86be63a commit 7c119d3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
const pTry = require('p-try');

const pLimit = concurrency => {
if (concurrency < 1) {
throw new TypeError('Expected `concurrency` to be a number from 1 and up');
if (!((Number.isInteger(concurrency) || concurrency === Infinity) && concurrency > 0)) {
return Promise.reject(new TypeError('Expected `concurrency` to be a number from 1 and up'));
}

const queue = [];
Expand Down
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ Returns a `limit` function.
#### concurrency

Type: `number`<br>
Minimum: `1`
Minimum: `1`<br>
Default: `Infinity`

Concurrency limit.

Expand Down
8 changes: 8 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,11 @@ test('activeCount and pendingCount properties', async t => {
t.is(limit.activeCount, 0);
t.is(limit.pendingCount, 0);
});

test('throws on invalid concurrency argument', async t => {
await t.throwsAsync(pLimit(0));
await t.throwsAsync(pLimit(-1));
await t.throwsAsync(pLimit(1.2));
await t.throwsAsync(pLimit(undefined));
await t.throwsAsync(pLimit(true));
});

0 comments on commit 7c119d3

Please sign in to comment.