18 lines
548 B
JavaScript
18 lines
548 B
JavaScript
/**
|
|
* Traite un tableau d'items avec au plus `concurrency` appels simultanés.
|
|
* @param {any[]} items
|
|
* @param {(item: any) => Promise<void>} processFn
|
|
* @param {number} concurrency
|
|
* @returns {Promise<void>}
|
|
*/
|
|
export async function processBatch(items, processFn, concurrency = 3) {
|
|
const queue = [...items];
|
|
const workers = Array.from({ length: concurrency }, async () => {
|
|
while (queue.length > 0) {
|
|
const item = queue.shift();
|
|
if (item !== undefined) await processFn(item);
|
|
}
|
|
});
|
|
await Promise.all(workers);
|
|
}
|