Created
July 21, 2022 08:20
-
-
Save dnafication/b01b2f5297e37cfdd81771ec14e10992 to your computer and use it in GitHub Desktop.
fastq example in nodejs. Task queue to run tasks concurrently with concurrency specified. Handle error occurred in worker fn.
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
const fastq = require("fastq"); | |
// sleep function | |
function sleep(ms) { | |
return new Promise((resolve) => setTimeout(resolve, ms)); | |
} | |
// worker function | |
async function worker(arg) { | |
console.log(`working on task ${arg}`); | |
await sleep(1000); | |
if (arg === 5) throw Error("bummer!!"); | |
console.log(`task ${arg} done`); | |
return arg * 2; | |
} | |
async function run() { | |
// initialize a task queue with function to be executed and concurrency | |
const taskQueue = fastq.promise(worker, 3); | |
const tasksArgs = Array.from(Array(10).keys()); | |
// array to collect tasks | |
const tasks = []; | |
tasksArgs.forEach((arg) => { | |
// push arg to worker and push the task to tasks array | |
tasks.push(taskQueue.push(arg)); | |
}); | |
const results = []; | |
// collect results | |
for (let task of tasks) { | |
try { | |
const result = await task; | |
results.push(result); | |
} catch (error) { | |
console.warn(error); | |
results.push("Failed"); | |
} | |
} | |
console.log(results); | |
} | |
run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment