sequential-task-queue
sequential-task-queue copied to clipboard
Priorities for the tasks
Argument options
of method push()
now has priority
field. Task with the lower value gets executed first.
Simple test:
const SequentialTaskQueue = require('sequential-task-queue').SequentialTaskQueue;
function job(n, pr) {
return new Promise(res => {
setTimeout(()=>{
console.log(`Task ${n} is finished with priority ${pr}.`);
res();
}, Math.random()*2000);
});
}
const queue = new SequentialTaskQueue({priority: 0});
for(let i = 0; i < 20; ++i) {
let pr = (Math.random()*100).toFixed(0);
queue.push(async () => {
await job(i, pr);
}, {priority: pr});
}
setTimeout(() => {
let pr = -10;
queue.push(async () => {
await job(999, pr);
}, {priority: pr});
console.log("High priority job added");
},4000);
Also, please add some unit tests.