Task Queue With setImmediate
NodeJS
Hard
4 views
Problem Description
Process an array of tasks without blocking the event loop.
Output Format
Print DONE.
Constraints
Use setImmediate to yield between chunks.
Official Solution
const tasks = Array.from({ length: 5000 }, (_, i) => i + 1);
let sum = 0;
function runChunk() {
const chunk = tasks.splice(0, 500);
for (const x of chunk) sum += x;
if (tasks.length) setImmediate(runChunk);
else console.log('DONE');
}
runChunk();
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!