A node minimalist queue module without dependencies
Allow you to queue CPU/memory intensive async (or sync) tasks.
$ npm install minimalist-queue
Here is simple example of using queue.
const { Queue, QueueEvents } = require("minimalist-queue");
const asyncTask = () => new Promise((resolve) => setTimeout(() => resolve("asyncTask"), 100));
const queue = new Queue();
queue.on(QueueEvents.TASK_DONE, (data) => {
console.log(`Result : ${data.result}`);
});
queue.addTask(asyncTask);
queue.start();
Class used to managing tasks
Type: number
Default: 5
Number of simultaneous tasks.
Type: number
Default: 120000
Time before task was forced to shutdown.
Type: boolean
Default: false
Queue will start automatically when a new task is added to the queue.
Create a new instance of a queue with given options, or use defaults if no options are given.
Type: Function
Return the list of added tasks.
Type: Function
Create a new task with the given task function and add it to the queue
Type: Function
Remove a given task of the queue.
Type: Function
Start the queue.
Type: Function
Clear the queue.
Class that containing all Queue events as static properties
Triggered when a task start
Triggered when a task is done
Triggered when a task failed to execute or encounter the queue timeout
Triggered when queue is starting
Triggered when queue is done
Here is a full example or minimalist-queue usage with sync / async (await included) and failed tasks
const { Queue, QueueEvents } = require("minimalist-queue");
const syncTask = () => "syncTask";
const asyncTask = () => new Promise((resolve) => setTimeout(() => resolve("asyncTask"), 100));
const awaitAsyncTask = async () => {
const promise = new Promise((resolve) => setTimeout(() => resolve("awaitAsyncTask"), 200));
return await promise;
};
const syncTaskFailed = () => _syncTaskFailed;
const asyncTaskFailed = () =>
new Promise((resolve, reject) =>
setTimeout(() => {
try {
resolve(_asyncTaskFailed);
} catch (error) {
reject(error);
}
}, 100)
);
const awaitAsyncTaskFailed = async () => {
const promise = new Promise((resolve, reject) => {
setTimeout(() => {
try {
resolve(_awaitAsyncTaskFailed);
} catch (error) {
reject(error);
}
}, 200);
});
return await promise;
};
const queue = new Queue();
queue.on(QueueEvents.START, () => {
console.log("\n+-------------------");
console.log("Queue started");
console.log("-------------------+");
});
queue.on(QueueEvents.DONE, () => {
console.log("\n=-------------------");
console.log("Queue done");
console.log("-------------------=");
});
queue.on(QueueEvents.TASK_DONE, (data) => {
console.log(`\x1b[33mTask done ! Result : ${data.result} in ${data.completionTime} ms\x1b[0m`);
});
queue.on(QueueEvents.TASK_FAILED, (data) => {
console.log("\nxxxxxxxxxxxxxxxxxxxx");
console.log(`\x1b[31mTask failed ! Error : ${data.result.message}\x1b[0m`);
console.log("xxxxxxxxxxxxxxxxxxxx\n");
});
queue.on(QueueEvents.TASK_START, (data) => {
console.log("\nTask start ! task details :", data.result, "\n");
});
queue.addTask(awaitAsyncTask);
queue.addTask(syncTask);
queue.addTask(asyncTask);
queue.addTask(awaitAsyncTaskFailed);
queue.addTask(syncTaskFailed);
queue.addTask(asyncTaskFailed);
queue.start();