Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit function #88

Merged
merged 11 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,22 @@ Run multiple promise-returning & async functions with limited concurrency.
@returns A `limit` function.
*/
export default function pLimit(concurrency: number): LimitFunction;

export type LimitFunctionOption = {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plural

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, just name it Options as I would like to make pLimit support an options-object too, for readability.

/**
Concurrency limit
*/
concurrency: number;
};

/**
Return a function with limited concurrency

@param function_ - Promise-returning/async function.
@param option
@return Function with limited concurrency
*/
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Return a function with limited concurrency
@param function_ - Promise-returning/async function.
@param option
@return Function with limited concurrency
*/
Return a function with limited concurrency.
@param function_ - Promise-returning/async function.
@return Function with limited concurrency.
*/

export function limitFunction<Arguments extends unknown[], ReturnType>(
function_: (...arguments_: Arguments) => PromiseLike<ReturnType> | ReturnType,
option: LimitFunctionOption
): (...arguments_: Arguments) => Promise<ReturnType>;
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ export default function pLimit(concurrency) {
return generator;
}

export function limitFunction(function_, option) {
const {concurrency} = option;
const limit = pLimit(concurrency);

return (...arguments_) => limit(() => function_(...arguments_));
}

function validateConcurrency(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
35 changes: 35 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ const result = await Promise.all(input);
console.log(result);
```

```js
import {limitFunction} from 'p-limit';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this example to the limitFunction API docs.


const limitFunction_ = limitFunction(async () => {
return doSomething();
}, {concurrency: 1});

const input = Array.from({length: 10}, limitFunction_);

// Only one promise is run at once
await Promise.all(input);
```

## API

### pLimit(concurrency)
Expand Down Expand Up @@ -77,6 +90,28 @@ Note: This does not cancel promises that are already running.

Get or set the concurrency limit.

### limitFunction(fn, options)

Return a function with limited concurrency
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Return a function with limited concurrency
Return a function with limited concurrency.

This needs to clearly describe how it's different from the main export.


#### fn

Type: `Function`

Promise-returning/async function.

#### options

Type: `object`

#### concurrency

Type: `number`(Interget)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo


Minimum: `1`

#### option
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leftover


## FAQ

### How is this different from the [`p-queue`](https://github.com/sindresorhus/p-queue) package?
Expand Down
18 changes: 17 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import delay from 'delay';
import inRange from 'in-range';
import timeSpan from 'time-span';
import randomInt from 'random-int';
import pLimit from './index.js';
import pLimit, {limitFunction} from './index.js';

test('concurrency: 1', async t => {
const input = [
Expand Down Expand Up @@ -217,3 +217,19 @@ test('change concurrency to bigger value', async t => {
await Promise.all(promises);
t.deepEqual(log, [1, 2, 3, 4, 4, 4, 4, 4, 4, 4]);
});

test('limit function with concurrency', async t => {
const concurrency = 5;
let running = 0;

const limitFunction_ = limitFunction(async () => {
running++;
t.true(running <= concurrency);
await delay(randomInt(30, 200));
running--;
}, {concurrency});

const input = Array.from({length: 100}, limitFunction_);

await Promise.all(input);
});
Loading