This is not GitHub.com

Like what you see? Install OctoLinker now!

Skip to content
Permalink
Browse files

Ensure `concurrency` is an integer

Fixes #26
  • Loading branch information
sindresorhus committed Mar 5, 2020
1 parent bf03769 commit b342717a9c44a2d3973b92a7cbf5eaef3a6252a2
Showing with 6 additions and 3 deletions.
  1. +2 −0 index.d.ts
  2. +2 −2 index.js
  3. +1 −1 readme.md
  4. +1 −0 test.js
@@ -3,6 +3,8 @@ declare namespace pMap {
/**
Number of concurrently pending promises returned by `mapper`.
Must be an integer from 1 and up or `Infinity`.
@default Infinity
*/
readonly concurrency?: number;
@@ -14,8 +14,8 @@ module.exports = async (
throw new TypeError('Mapper function is required');
}

if (!(typeof concurrency === 'number' && concurrency >= 1)) {
throw new TypeError(`Expected \`concurrency\` to be a number from 1 and up, got \`${concurrency}\` (${typeof concurrency})`);
if (!((Number.isSafeInteger(concurrency) || concurrency === Infinity) && concurrency >= 1)) {
throw new TypeError(`Expected \`concurrency\` to be an integer from 1 and up or \`Infinity\`, got \`${concurrency}\` (${typeof concurrency})`);
}

const result = [];
@@ -59,7 +59,7 @@ Type: `object`

##### concurrency

Type: `number`\
Type: `number` (Integer)\
Default: `Infinity`\
Minimum: `1`

@@ -91,6 +91,7 @@ test('async with concurrency: 2 (out of order time sequence)', async t => {

test('enforce number in options.concurrency', async t => {
await t.throwsAsync(pMap([], () => {}, {concurrency: 0}), TypeError);
await t.throwsAsync(pMap([], () => {}, {concurrency: 1.5}), TypeError);
await t.notThrowsAsync(pMap([], () => {}, {concurrency: 1}));
await t.notThrowsAsync(pMap([], () => {}, {concurrency: 10}));
await t.notThrowsAsync(pMap([], () => {}, {concurrency: Infinity}));

0 comments on commit b342717

Please sign in to comment.
You can’t perform that action at this time.