1 | export type LimitFunction = {
|
2 | /**
|
3 | The number of promises that are currently running.
|
4 | */
|
5 | readonly activeCount: number;
|
6 |
|
7 | /**
|
8 | The number of promises that are waiting to run (i.e. their internal `fn` was not called yet).
|
9 | */
|
10 | readonly pendingCount: number;
|
11 |
|
12 | /**
|
13 | Get or set the concurrency limit.
|
14 | */
|
15 | concurrency: number;
|
16 |
|
17 | /**
|
18 | Discard pending promises that are waiting to run.
|
19 |
|
20 | 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.
|
21 |
|
22 | Note: This does not cancel promises that are already running.
|
23 | */
|
24 | clearQueue: () => void;
|
25 |
|
26 | /**
|
27 | @param fn - Promise-returning/async function.
|
28 | @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.
|
29 | @returns The promise returned by calling `fn(...arguments)`.
|
30 | */
|
31 | <Arguments extends unknown[], ReturnType>(
|
32 | function_: (...arguments_: Arguments) => PromiseLike<ReturnType> | ReturnType,
|
33 | ...arguments_: Arguments
|
34 | ): Promise<ReturnType>;
|
35 | };
|
36 |
|
37 | /**
|
38 | Run multiple promise-returning & async functions with limited concurrency.
|
39 |
|
40 | @param concurrency - Concurrency limit. Minimum: `1`.
|
41 | @returns A `limit` function.
|
42 | */
|
43 | export default function pLimit(concurrency: number): LimitFunction;
|
44 |
|
45 | export type Options = {
|
46 | /**
|
47 | Concurrency limit.
|
48 |
|
49 | Minimum: `1`.
|
50 | */
|
51 | readonly concurrency: number;
|
52 | };
|
53 |
|
54 | /**
|
55 | Returns a function with limited concurrency.
|
56 |
|
57 | The returned function manages its own concurrent executions, allowing you to call it multiple times without exceeding the specified concurrency limit.
|
58 |
|
59 | Ideal for scenarios where you need to control the number of simultaneous executions of a single function, rather than managing concurrency across multiple functions.
|
60 |
|
61 | @param function_ - Promise-returning/async function.
|
62 | @return Function with limited concurrency.
|
63 |
|
64 | @example
|
65 | ```
|
66 | import {limitFunction} from 'p-limit';
|
67 |
|
68 | const limitedFunction = limitFunction(async () => {
|
69 | return doSomething();
|
70 | }, {concurrency: 1});
|
71 |
|
72 | const input = Array.from({length: 10}, limitedFunction);
|
73 |
|
74 | // Only one promise is run at once.
|
75 | await Promise.all(input);
|
76 | ```
|
77 | */
|
78 | export function limitFunction<Arguments extends unknown[], ReturnType>(
|
79 | function_: (...arguments_: Arguments) => PromiseLike<ReturnType> | ReturnType,
|
80 | option: Options
|
81 | ): (...arguments_: Arguments) => Promise<ReturnType>;
|