Skip to content

Commit

Permalink
Require Node.js 18
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 1, 2023
1 parent ad8afe6 commit 23d61ba
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 39 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ jobs:
fail-fast: false
matrix:
node-version:
- 16
- 20
- 18
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
10 changes: 4 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/* eslint-disable @typescript-eslint/member-ordering */

export interface LimitFunction {
export type LimitFunction = {
/**
The number of promises that are currently running.
*/
Expand All @@ -26,10 +24,10 @@ export interface LimitFunction {
@returns The promise returned by calling `fn(...arguments)`.
*/
<Arguments extends unknown[], ReturnType>(
fn: (...arguments: Arguments) => PromiseLike<ReturnType> | ReturnType,
...arguments: Arguments
fn: (...arguments_: Arguments) => PromiseLike<ReturnType> | ReturnType,
...arguments_: Arguments
): Promise<ReturnType>;
}
};

/**
Run multiple promise-returning & async functions with limited concurrency.
Expand Down
14 changes: 7 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ export default function pLimit(concurrency) {
}
};

const run = async (fn, resolve, args) => {
const run = async (function_, resolve, arguments_) => {
activeCount++;

const result = (async () => fn(...args))();
const result = (async () => function_(...arguments_))();

resolve(result);

Expand All @@ -31,9 +31,9 @@ export default function pLimit(concurrency) {
next();
};

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

(async () => {
Expand All @@ -49,8 +49,8 @@ export default function pLimit(concurrency) {
})();
};

const generator = (fn, ...args) => new Promise(resolve => {
enqueue(fn, resolve, args);
const generator = (function_, ...arguments_) => new Promise(resolve => {
enqueue(function_, resolve, arguments_);
});

Object.defineProperties(generator, {
Expand All @@ -61,7 +61,7 @@ export default function pLimit(concurrency) {
get: () => queue.size,
},
clearQueue: {
value: () => {
value() {
queue.clear();
},
},
Expand Down
17 changes: 10 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"exports": {
"types": "./index.d.ts",
"default": "./index.js"
},
"imports": {
"#async_hooks": {
"node": "async_hooks",
"default": "./async-hooks-stub.js"
}
},
"engines": {
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
"node": ">=18"
},
"scripts": {
"test": "xo && ava && tsd"
Expand Down Expand Up @@ -50,12 +53,12 @@
"yocto-queue": "^1.0.0"
},
"devDependencies": {
"ava": "^3.15.0",
"delay": "^5.0.0",
"ava": "^5.3.1",
"delay": "^6.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"
"time-span": "^5.1.0",
"tsd": "^0.29.0",
"xo": "^0.56.0"
}
}
19 changes: 4 additions & 15 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

> Run multiple promise-returning & async functions with limited concurrency
*Works in Node.js and browsers.*

## Install

```
$ npm install p-limit
```sh
npm install p-limit
```

## Usage
Expand Down Expand Up @@ -80,20 +82,7 @@ This package is only about limiting the number of concurrent executions, while `

## Related

- [p-queue](https://github.com/sindresorhus/p-queue) - Promise queue with concurrency control
- [p-throttle](https://github.com/sindresorhus/p-throttle) - Throttle promise-returning & async functions
- [p-debounce](https://github.com/sindresorhus/p-debounce) - Debounce promise-returning & async functions
- [p-all](https://github.com/sindresorhus/p-all) - Run promise-returning & async functions concurrently with optional limited concurrency
- [More…](https://github.com/sindresorhus/promise-fun)

---

<div align="center">
<b>
<a href="https://tidelift.com/subscription/pkg/npm-p-limit?utm_source=npm-p-limit&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
</b>
<br>
<sub>
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
</sub>
</div>
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import delay from 'delay';
import inRange from 'in-range';
import timeSpan from 'time-span';
import randomInt from 'random-int';
import {AsyncLocalStorage} from '#async_hooks';
import pLimit from './index.js';
import {AsyncLocalStorage} from '#async_hooks';

test('concurrency: 1', async t => {
const input = [
Expand Down

0 comments on commit 23d61ba

Please sign in to comment.