diff --git a/lib/plugins/s3/limit.js b/lib/plugins/s3/limit.js index 2d01b7513..e72f50ce5 100644 --- a/lib/plugins/s3/limit.js +++ b/lib/plugins/s3/limit.js @@ -1,34 +1,15 @@ export async function runWithConcurrencyLimit(tasks, limit) { - const running = new Set(); - - async function runNext() { - if (tasks.length === 0) { - return; - } - - const task = tasks.shift(); - const promise = task() - .catch(error => { + async function worker() { + while (tasks.length > 0) { + const task = tasks.shift(); + try { + await task(); + } catch (error) { throw error; - }) - .finally(() => { - running.delete(promise); - void runNext(); - }); - - running.add(promise); - if (running.size < limit) { - void runNext(); + } } } - const starters = []; - for (let index = 0; index < limit && tasks.length > 0; index++) { - starters.push(runNext()); - } - - await Promise.allSettled(starters); - if (running.size > 0) { - await Promise.allSettled(Array.from(running)); - } + const workers = Array.from({ length: limit }, () => worker()); + await Promise.all(workers); }