JavaScript
常见代码片段
Lazyman
class LazyMan {
  constructor(name) {
    this.name = name;
    this.task = []; // 任务队列
    console.log(`My named ${name}`);
 
    // 这里使用异步调用next()是为了确保所有链式调用都被添加到task[]才开始执行任务
    setTimeout(() => {
      this.next();
    });
  }
 
  sleep(time) {
    this.task.push(() => {
      console.log(`I am sleeping...`);
      setTimeout(() => {
        console.log(`after ${time} ms`);
        this.next();
      }, time);
    });
    return this;
  }
 
  eat(food) {
    this.task.push(() => {
      console.log(food);
      this.next();
    });
    return this;
  }
 
  next() {
    let fn = this.task.shift();
    fn && fn(); // if(fn) fn()
  }
}
 
const lazyMan = new LazyMan("jack");
lazyMan.eat("apple").sleep(5000).eat("hamburger").sleep(3000).eat("pear");
// My named jack
// apple
// I am sleeping...
// after 5000 ms
// hamburger
// I am sleeping...
// after 3000 ms
// pear