Open
Description
class Scheduler {
constructor(count) {
this.count = 2
this.queue = []
this.run = []
}
add(task) {
this.queue.push(task)
return this.schedule()
}
schedule() {
if (this.run.length < this.count && this.queue.length) {
const task = this.queue.shift()
const promise = task().then(() => {
this.run.splice(this.run.indexOf(promise), 1)
})
this.run.push(promise)
return promise
} else {
return Promise.race(this.run).then(() => this.schedule())
}
}
}
Metadata
Metadata
Assignees
Labels
No labels