lambda-live-debugger
Version:
Debug Lambda functions locally like it is running in the cloud
20 lines (19 loc) • 662 B
JavaScript
/**
* Run async tasks with a limited number of tasks running concurrently,
* for example to avoid hitting AWS API rate limits.
* @param tasks
* @param limit
* @returns results in the same order as the tasks
*/
export async function runWithConcurrency(tasks, limit = 5) {
const results = new Array(tasks.length);
let nextTaskIndex = 0;
const workers = Array.from({ length: Math.min(limit, tasks.length) }, async () => {
while (nextTaskIndex < tasks.length) {
const taskIndex = nextTaskIndex++;
results[taskIndex] = await tasks[taskIndex]();
}
});
await Promise.all(workers);
return results;
}