当任务函数执行next的时候,跳转到下一个任务函数
class Task {
constructor() {
this.taskIndex = 0
this.taskList = []
this.stopRun = false
}
add(fn,context, ...args) {
const next = () => {
this.taskIndex++
if (!this.stopRun && this.taskList[this.taskIndex]) {
this.run()
}
}
this.taskList.push(fn.bind(context, next, ...args))
return this
}
run() {
this.taskList[this.taskIndex]()
}
stop() {
this.stopRun = true
}
}
function task1(next) {
setTimeout(() => {
console.log(1)
next()
},1000)
}
function task2(next,b) {
setTimeout(() => {
console.log(b)
next()
},1000)
}
let task = new Task()
task.add(task1).add(task2,null,3)
task.run()