创建一个异步的类

当任务函数执行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()

 

posted @ 2019-08-28 17:16  super_素素  阅读(263)  评论(0编辑  收藏  举报