promise-pool.ts
1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* Run multiple asynchronous tasks with a concurrency limit.
*
* @param items - Array of items to process
* @param taskGenerator - Function that creates a promise for a single item
* @param concurrency - Maximum number of concurrent tasks
* @returns Array of results
*/
export async function promisePool<T, R>(items: T[], taskGenerator: (item: T) => Promise<R>, concurrency: number = 5): Promise<R[]> {
if (!items || !items.length) {
return [];
}
const results: R[] = [];
let nextIndex = 0;
const executing = new Set<Promise<void>>();
const processTask = async (index: number) => {
const item = items[index];
if (!item) {
results[index] = undefined as unknown as R;
return;
}
const result = await taskGenerator(item);
results[index] = result;
};
while (nextIndex < items.length || executing.size > 0) {
while (nextIndex < items.length && executing.size < concurrency) {
const index = nextIndex++;
const promise = processTask(index).finally(() => {
executing.delete(promise);
});
executing.add(promise);
}
if (executing.size > 0) {
await Promise.race(executing);
}
}
return results;
}