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 3 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
18 changes: 18 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,21 @@ Run multiple promise-returning & async functions with limited concurrency.
@returns A `limit` function.
*/
export default function pLimit(concurrency: number): LimitFunction;

export type Options = {
/**
Concurrency limit
*/
concurrency: number;
};

/**
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: Options
): (...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
34 changes: 34 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,40 @@ 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.

Note: Unlike fucntion return by `pLimit`, the return by `limitFunction` is already limited, do not need call with `limit`.
Copy link
Owner

Choose a reason for hiding this comment

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

Multiple typos and does not make it super clear what the difference is. Maybe try using ChatGPT.

Copy link
Owner

Choose a reason for hiding this comment

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

Should be in index.d.ts too


#### fn

Type: `Function`

Promise-returning/async function.

#### options

Type: `object`

#### concurrency

Type: `number`\
Minimum: `1`

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

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);
```

## 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