← Каталог
Примеры решений в TypeScript — 4. Ограничение параллелизма
Фрагмент из «Примеры решений в TypeScript»: 4. Ограничение параллелизма.
async function mapLimit<T, R>(
items: T[],
limit: number,
fn: (item: T) => Promise<R>
): Promise<R[]> {
const results: R[] = [];
const queue = [..items];
const workers = Array.from({ length: Math.min(limit, items.length) }, async () => {
while (queue.length) {
const item = queue.shift()!;
results.push(await fn(item));
}
});
await Promise.all(workers);
return results;
} async function mapLimit<T, R>(
items: T[],
limit: number,
fn: (item: T) => Promise<R>
): Promise<R[]> {
const results: R[] = [];
const queue = [..items];
const workers = Array.from({ length: Math.min(limit, items.length) }, async () => {
while (queue.length) {
const item = queue.shift()!;
results.push(await fn(item));
}
});
await Promise.all(workers);
return results;
}