首页 > 其他 > 详细

创建一个异步的类

时间:2019-08-28 17:40:15      阅读:89      评论:0      收藏:0      [点我收藏+]
当任务函数执行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()

 

创建一个异步的类

原文:https://www.cnblogs.com/woniubushinide/p/11425234.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!