Skip to content

Instantly share code, notes, and snippets.

@dnafication
Created July 21, 2022 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dnafication/b01b2f5297e37cfdd81771ec14e10992 to your computer and use it in GitHub Desktop.
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.
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