Created
October 9, 2024 15:11
-
-
Save kirkegaard/bc5eb515ffc72eadddaf7d2a54f116bb to your computer and use it in GitHub Desktop.
JobQueue using pure async/await
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A job queue that takes an anonymous functions and executes them in parallel | |
let maxConcurrent = 3; | |
let queue = []; | |
let activeCount = 0; | |
// Add task to queue and execute | |
async function exec(task) { | |
queue.push(task); | |
await next(); | |
} | |
// Process the next task in the queue | |
async function next() { | |
if (activeCount < maxConcurrent && queue.length > 0) { | |
const nextTask = queue.shift(); | |
activeCount++; | |
try { | |
// Await the task's execution and propagate the result (resolve or reject) | |
await nextTask(); | |
} catch (error) { | |
// Handle errors or add retry logic here | |
console.error("Task failed:", error); | |
} finally { | |
activeCount--; | |
next(); // Process the next task once this one is finished | |
} | |
} | |
} | |
const setMaxConcurrent = (n) => { | |
maxConcurrent = n; | |
}; | |
module.export = { exec, setMaxConcurrent }; | |
// Usage | |
const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); | |
const Job = async (id, delay) => { | |
console.log(`Task ${id} added`); | |
await sleep(delay); | |
if (id === 2) { | |
throw new Error("err happened"); | |
} | |
console.log(`Task ${id} done`); | |
}; | |
exec(() => Job(1, 1000)); | |
exec(() => Job(2, 750)); | |
exec(() => Job(3, 2000)); | |
exec(() => Job(4, 2500)); | |
exec(() => Job(5, 1000)); | |
exec(() => Job(6, 250)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment