LazyMan

2021-07-09  本文已影响0人  Ag_fronted
class LazyMan {
  constructor(name) {
    this.tasks = [
      () => {
        console.log(`Hi! This is ${name}`);
        this.next();
      },
    ];
    setTimeout(() => {
      this.next();
    }, 0);
  }

  next() {
    const task = this.tasks.shift();
    task && task();
  }

  sleep(time) {
    this.tasks.push(() => {
      setTimeout(() => {
        console.log(`Wake up after ${time}`);
        this.next();
      }, time * 1000);
    });
    return this;
  }

  eat(name) {
    this.tasks.push(() => {
      console.log(`Eat ${name}`);
      this.next();
    });
    return this;
  }
}

new LazyMan("小花").eat("西瓜").sleep(5).eat("桃子").eat(1);

上一篇 下一篇

猜你喜欢

热点阅读