Comparing version 6.1.0 to 6.2.0
@@ -44,1 +44,39 @@ export type LimitFunction = { | ||
export default function pLimit(concurrency: number): LimitFunction; | ||
export type Options = { | ||
/** | ||
Concurrency limit. | ||
Minimum: `1`. | ||
*/ | ||
readonly concurrency: number; | ||
}; | ||
/** | ||
Returns a function with limited concurrency. | ||
The returned function manages its own concurrent executions, allowing you to call it multiple times without exceeding the specified concurrency limit. | ||
Ideal for scenarios where you need to control the number of simultaneous executions of a single function, rather than managing concurrency across multiple functions. | ||
@param function_ - Promise-returning/async function. | ||
@return Function with limited concurrency. | ||
@example | ||
``` | ||
import {limitFunction} from 'p-limit'; | ||
const limitedFunction = limitFunction(async () => { | ||
return doSomething(); | ||
}, {concurrency: 1}); | ||
const input = Array.from({length: 10}, limitedFunction); | ||
// Only one promise is run at once. | ||
await Promise.all(input); | ||
``` | ||
*/ | ||
export function limitFunction<Arguments extends unknown[], ReturnType>( | ||
function_: (...arguments_: Arguments) => PromiseLike<ReturnType> | ReturnType, | ||
option: Options | ||
): (...arguments_: Arguments) => Promise<ReturnType>; |
@@ -93,2 +93,9 @@ import Queue from 'yocto-queue'; | ||
export function limitFunction(function_, option) { | ||
const {concurrency} = option; | ||
const limit = pLimit(concurrency); | ||
return (...arguments_) => limit(() => function_(...arguments_)); | ||
} | ||
function validateConcurrency(concurrency) { | ||
@@ -95,0 +102,0 @@ if (!((Number.isInteger(concurrency) || concurrency === Number.POSITIVE_INFINITY) && concurrency > 0)) { |
{ | ||
"name": "p-limit", | ||
"version": "6.1.0", | ||
"version": "6.2.0", | ||
"description": "Run multiple promise-returning & async functions with limited concurrency", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -33,3 +33,3 @@ # p-limit | ||
### pLimit(concurrency) | ||
### pLimit(concurrency) <sup>default export</sup> | ||
@@ -41,4 +41,3 @@ Returns a `limit` function. | ||
Type: `number`\ | ||
Minimum: `1`\ | ||
Default: `Infinity` | ||
Minimum: `1` | ||
@@ -83,2 +82,40 @@ Concurrency limit. | ||
### limitFunction(fn, options) <sup>named export</sup> | ||
Returns a function with limited concurrency. | ||
The returned function manages its own concurrent executions, allowing you to call it multiple times without exceeding the specified concurrency limit. | ||
Ideal for scenarios where you need to control the number of simultaneous executions of a single function, rather than managing concurrency across multiple functions. | ||
```js | ||
import {limitFunction} from 'p-limit'; | ||
const limitedFunction = limitFunction(async () => { | ||
return doSomething(); | ||
}, {concurrency: 1}); | ||
const input = Array.from({length: 10}, limitedFunction); | ||
// Only one promise is run at once. | ||
await Promise.all(input); | ||
``` | ||
#### fn | ||
Type: `Function` | ||
Promise-returning/async function. | ||
#### options | ||
Type: `object` | ||
#### concurrency | ||
Type: `number`\ | ||
Minimum: `1` | ||
Concurrency limit. | ||
## FAQ | ||
@@ -85,0 +122,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10346
147
130