Skip to content

Commit

Permalink
Fix error on invalid concurrency (#35)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
sethp and sindresorhus authored Jul 12, 2020
1 parent 824edb8 commit 4ab2813
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const pTry = require('p-try');

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

const queue = [];
Expand Down
26 changes: 20 additions & 6 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,24 @@ test('clearQueue', async t => {
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));
test('throws on invalid concurrency argument', t => {
t.throws(() => {
pLimit(0);
});

t.throws(() => {
pLimit(-1);
});

t.throws(() => {
pLimit(1.2);
});

t.throws(() => {
pLimit(undefined);
});

t.throws(() => {
pLimit(true);
});
});

0 comments on commit 4ab2813

Please sign in to comment.