sequential-task-queue icon indicating copy to clipboard operation
sequential-task-queue copied to clipboard

Priorities for the tasks

Open AlexIII opened this issue 6 years ago • 1 comments

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);

AlexIII avatar Oct 17 '18 10:10 AlexIII

Also, please add some unit tests.

BalassaMarton avatar Dec 01 '18 09:12 BalassaMarton