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
// 实现带异步任务的链式调用:chain.eat().sleep(5).work().eat().work().sleep(10) | |
// 链式调用:函数都 return this | |
// 链式调用时(同步过程),会将每个任务(函数)放到 队列中(tasks) | |
// 使用 start 函数异步开始执行第一个任务( next 函数第一次调用) 每一个任务执行完就 再调用 next 函数,next 会取出下一个任务出来执行 | |
class Chaos { | |
constructor() { | |
this.tasks = [] | |
const fn = () => { | |
console.log('chain start') | |
this.next() |