class LazyMan { tasks = [] constructor(name){ this.name = name console.log(`I am ${name}`) setTimeout(() => { this.next() }) } next(){ const task = this.tasks.shift() if(task) task() } do(sth){ const task = () => { console.log(`do ${sth}`) this.next() } this.tasks.push(task) return this } sleep(delay){ const task = () => { setTimeout(() => { console.log(`sleep ${delay}s`) this.next() }, delay * 1000) } this.tasks.push(task) return this } }
标签:tasks,console,name,next,delay,task,LazyMan From: https://www.cnblogs.com/zhenjianyu/p/17086815.html