Skip to content

Commit

Permalink
Require Node.js 12.20 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Aug 12, 2021
1 parent cada94e commit 9e08401
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 73 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ jobs:
fail-fast: false
matrix:
node-version:
- 14
- 12
- 10
- 16
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
66 changes: 32 additions & 34 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
declare namespace pLimit {
interface Limit {
/**
The number of promises that are currently running.
*/
readonly activeCount: number;

/**
The number of promises that are waiting to run (i.e. their internal `fn` was not called yet).
*/
readonly pendingCount: number;

/**
Discard pending promises that are waiting to run.
This might be useful if you want to teardown the queue at the end of your program's lifecycle or discard any function calls referencing an intermediary state of your app.
Note: This does not cancel promises that are already running.
*/
clearQueue: () => void;

/**
@param fn - Promise-returning/async function.
@param arguments - Any arguments to pass through to `fn`. Support for passing arguments on to the `fn` is provided in order to be able to avoid creating unnecessary closures. You probably don't need this optimization unless you're pushing a lot of functions.
@returns The promise returned by calling `fn(...arguments)`.
*/
<Arguments extends unknown[], ReturnType>(
fn: (...arguments: Arguments) => PromiseLike<ReturnType> | ReturnType,
...arguments: Arguments
): Promise<ReturnType>;
}
/* eslint-disable @typescript-eslint/member-ordering */

export interface LimitFunction {
/**
The number of promises that are currently running.
*/
readonly activeCount: number;

/**
The number of promises that are waiting to run (i.e. their internal `fn` was not called yet).
*/
readonly pendingCount: number;

/**
Discard pending promises that are waiting to run.
This might be useful if you want to teardown the queue at the end of your program's lifecycle or discard any function calls referencing an intermediary state of your app.
Note: This does not cancel promises that are already running.
*/
clearQueue: () => void;

/**
@param fn - Promise-returning/async function.
@param arguments - Any arguments to pass through to `fn`. Support for passing arguments on to the `fn` is provided in order to be able to avoid creating unnecessary closures. You probably don't need this optimization unless you're pushing a lot of functions.
@returns The promise returned by calling `fn(...arguments)`.
*/
<Arguments extends unknown[], ReturnType>(
fn: (...arguments: Arguments) => PromiseLike<ReturnType> | ReturnType,
...arguments: Arguments
): Promise<ReturnType>;
}

/**
Expand All @@ -37,6 +37,4 @@ Run multiple promise-returning & async functions with limited concurrency.
@param concurrency - Concurrency limit. Minimum: `1`.
@returns A `limit` function.
*/
declare function pLimit(concurrency: number): pLimit.Limit;

export = pLimit;
export default function pLimit(concurrency: number): LimitFunction;
21 changes: 9 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict';
const Queue = require('yocto-queue');
import Queue from 'yocto-queue';

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

Expand Down Expand Up @@ -32,7 +31,7 @@ const pLimit = concurrency => {
};

const enqueue = (fn, resolve, args) => {
queue.enqueue(run.bind(null, fn, resolve, args));
queue.enqueue(run.bind(undefined, fn, resolve, args));

(async () => {
// This function needs to wait until the next microtask before comparing
Expand All @@ -53,19 +52,17 @@ const pLimit = concurrency => {

Object.defineProperties(generator, {
activeCount: {
get: () => activeCount
get: () => activeCount,
},
pendingCount: {
get: () => queue.size
get: () => queue.size,
},
clearQueue: {
value: () => {
queue.clear();
}
}
},
},
});

return generator;
};

module.exports = pLimit;
}
8 changes: 4 additions & 4 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import {expectType} from 'tsd';
import pLimit = require('.');
import pLimit from './index.js';

const limit = pLimit(1);

const input = [
limit(async () => 'foo'),
limit(async () => 'bar'),
limit(async () => undefined)
limit(async () => undefined),
];

expectType<Promise<Array<string | undefined>>>(Promise.all(input));

expectType<Promise<string>>(limit((a: string) => '', 'test'));
expectType<Promise<string>>(limit(async (a: string, b: number) => '', 'test', 1));
expectType<Promise<string>>(limit((_a: string) => '', 'test'));
expectType<Promise<string>>(limit(async (_a: string, _b: number) => '', 'test', 1));

expectType<number>(limit.activeCount);
expectType<number>(limit.pendingCount);
Expand Down
20 changes: 11 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=10"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -38,15 +40,15 @@
"bluebird"
],
"dependencies": {
"yocto-queue": "^0.1.0"
"yocto-queue": "^1.0.0"
},
"devDependencies": {
"ava": "^2.4.0",
"delay": "^4.4.0",
"in-range": "^2.0.0",
"random-int": "^2.0.1",
"time-span": "^4.0.0",
"tsd": "^0.13.1",
"xo": "^0.35.0"
"ava": "^3.15.0",
"delay": "^5.0.0",
"in-range": "^3.0.0",
"random-int": "^3.0.0",
"time-span": "^5.0.0",
"tsd": "^0.17.0",
"xo": "^0.44.0"
}
}
10 changes: 4 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ $ npm install p-limit
## Usage

```js
const pLimit = require('p-limit');
import pLimit from 'p-limit';

const limit = pLimit(1);

Expand All @@ -21,11 +21,9 @@ const input = [
limit(() => doSomething())
];

(async () => {
// Only one promise is run at once
const result = await Promise.all(input);
console.log(result);
})();
// Only one promise is run at once
const result = await Promise.all(input);
console.log(result);
```

## API
Expand Down
8 changes: 4 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import delay from 'delay';
import inRange from 'in-range';
import timeSpan from 'time-span';
import randomInt from 'random-int';
import pLimit from '.';
import pLimit from './index.js';

test('concurrency: 1', async t => {
const input = [
[10, 300],
[20, 200],
[30, 100]
[30, 100],
];

const end = timeSpan();
Expand Down Expand Up @@ -57,7 +57,7 @@ test('continues after sync throw', async t => {
}),
limit(() => {
ran = true;
})
}),
];

await Promise.all(promises).catch(() => {});
Expand Down Expand Up @@ -86,7 +86,7 @@ test('does not ignore errors', async t => {
}),
limit(async () => {
await delay(50);
})
}),
];

await t.throwsAsync(Promise.all(promises), {is: error});
Expand Down

0 comments on commit 9e08401

Please sign in to comment.