A Pen by Karan Jariwala on CodePen.
Last active
November 6, 2017 18:28
-
-
Save karanjariwala/15f86bb7dc655d491913348b18fd8030 to your computer and use it in GitHub Desktop.
Task Runner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
A Pen by Karan Jariwala on CodePen.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Task Runner using ES6