I want to implement something like a task runner which will be pushed new tasks. Each of those tasks could be some async operation like waiting on user or making API calls or something else. The task runner makes sure that at a time only allowed number of tasks can execute, while other tasks will keep on waiting till their turn comes.
class Runner {
constructor(concurrent) {
this.taskQueue = []; //this should have "concurrent" number of tasks running at any given time
}
push(task) {
/* pushes to the queue and then runs the whole queue */
}
}
The calling pattern would be
let runner = new Runner(3);
runner.push(task1);
runner.push(task2);
runner.push(task3);
runner.push(task4);
where task is a function reference which will run a callback at the end by which we may know that it is finished. So it should be like
let task = function(callback) {
/* does something which is waiting on IO or network or something else*/
callback();
}
So I am pushing a closure to runner like
runner.push(function(){return task(callback);});
I think I might need to add a waitList queue as well. But the tasks are not promise itself, so I don't know how to check if those are finished.
Anyways, I need the right approach.
async
function). – Teammate