Skip to content

Instantly share code, notes, and snippets.

@karanjariwala
Last active November 6, 2017 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karanjariwala/15f86bb7dc655d491913348b18fd8030 to your computer and use it in GitHub Desktop.
Save karanjariwala/15f86bb7dc655d491913348b18fd8030 to your computer and use it in GitHub Desktop.
Task Runner
class TaskRunner{
constructor(num){
this.num=num;
this.exeCount=0;
this.buff=[];
}
done(){
this.exeCount--;
if(this.buff.length>0){
this.exeCount++;
this.buff[0](this.done.bind(this))
this.buff=this.buff.slice(1);
}
}
push(task){
if(this.exeCount<this.num){
this.exeCount++;
task(this.done.bind(this))
}else{
this.buff.push(task)
}
}
}
let a=0
function example(done){
let myNum=a++;
console.log('executing function'+myNum)
setTimeout(()=>{
console.log('exeComplete'+myNum)
done();
},2000)
}
let taskRunner = new TaskRunner(4);
taskRunner.push(example);
taskRunner.push(example);
taskRunner.push(example);
taskRunner.push(example);
taskRunner.push(example);
taskRunner.push(example);
taskRunner.push(example);
taskRunner.push(example);
taskRunner.push(example);
@karanjariwala
Copy link
Author

Task Runner using ES6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment