class Lazyman {
  constructor() {
    this.tasks = [];
    this.init();
  }

  init() {
    const task = () => {
      console.log('i am a lazyman');
      this.next();
    };
    this.tasks.unshift(task);
    setTimeout(() => {
      this.next();
    }, 0);
  }

  next() {
    if (this.tasks.length) this.tasks.shift()();
  }

  sleep(timer) {
    const task = () => {
      setTimeout(() => {
        console.log(`sleep ${timer}s`);
        this.next();
      }, timer * 1000);
    };
    this.tasks.push(task);
    return this;
  }

  eat(food) {
    const task = () => {
      console.log(`eat ${food}`);
    };
    this.tasks.push(task);
    return this;
  }
}

const aMan = new Lazyman();
aMan
  .sleep(1)
  .eat('orange')
  .sleep(2);

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。