diff --git a/src/examples/js/concurrent.js b/src/examples/js/concurrent.js index 3eed0d2..4e11c32 100644 --- a/src/examples/js/concurrent.js +++ b/src/examples/js/concurrent.js @@ -16,18 +16,19 @@ const processTask = async (todo, batchId) => { const executeConcurrent = async (todos, maxBatchSize) => { console.log(`\ndo <= ${maxBatchSize} of ${todos.length} tasks (concurrently), at any given time\n`); const done = []; - const todosCopy = todos.slice(); + const worker = async (_, batchIndex) => { - let todo = todosCopy.shift(); - while (todo) { + let todo = todos.shift(); + while (todo !== undefined) { const [result] = await Promise.allSettled([processTask(todo, batchIndex + 1)]); done.push(result); - todo = todosCopy.shift(); + todo = todos.shift(); } }; - const batchStarters = Array.from({length: maxBatchSize}, worker); - await Promise.all(batchStarters); + const batches = Array.from({length: maxBatchSize}, worker); + await Promise.allSettled(batches); + return done; }; diff --git a/src/examples/js/without-lib.js b/src/examples/js/without-lib.js index 5f7de65..f2f033c 100644 --- a/src/examples/js/without-lib.js +++ b/src/examples/js/without-lib.js @@ -13,7 +13,6 @@ const processTask = async (todo, batchId) => { return out; }; -// Everywhere start/end is used is repititive and error prone const executeWithoutLibrary = async (todos, maxBatchSize) => { console.log(`\ndo <= ${maxBatchSize} of ${todos.length} tasks, at any given time (no library)`); const done = [];