Asynchronous (asyn-await/promise) function queue.
import { Queue } from '@ex1st/tiny-queue';
const queue = new Queue(async function (task) {
const r = await task;
return r;
}, { timeout: 2000 });
const result = [];
// add task to the queue
queue.push(new Promise(resolve => {
setTimeout(() => {
resolve(1);
}, 1000);
}));
// add task to the queue
queue.push(new Promise(resolve => {
setTimeout(() => {
resolve(2);
}, 500);
}));
// add task to the queue
queue.push(new Promise(resolve => {
setTimeout(() => {
resolve(3);
}, 100);
}), new Promise(resolve => {
setTimeout(() => {
resolve(4);
}, 50);
}));
// add task to the queue
queue.push(new Promise(resolve => {
setTimeout(() => {
resolve(5);
}, 2500);
}));
queue.on('done', function (res, task) {
result.push(res);
});
queue.on('timeout', function (task) {
console.log('task timed out:', task);
});
console.log(queue.running()); // true
console.log(queue.length()); // 4
queue.on('drain', function () {
console.log(result); // [1,2,3,4]
});
npm install @ex1st/tiny-queue
worker
functiontimeout
integer, is optionalfilter
function, is optional
add new task(s) to the queue.
a function that pauses the processing of tasks.
a function that resumes the processing of the queued tasks.
a function that empties remaining tasks from the queue
a function returning execution state of the queue
a function returning the queue's size
a function set a custom filter
When a task is done.
When a task throws an error.
When timeout params milliseconds have elapsed.
When a last task from a queue
has returned from a worker
When a last task from a queue
is given to a worker