Skip to content

Commit

Permalink
use example code
Browse files Browse the repository at this point in the history
  • Loading branch information
chalu committed Mar 18, 2024
1 parent ba54c43 commit c88d226
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
13 changes: 7 additions & 6 deletions src/examples/js/concurrent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
1 change: 0 additions & 1 deletion src/examples/js/without-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down

0 comments on commit c88d226

Please sign in to comment.