SOURCE

function lyc(name){
    this.task = [];
    let fn = () => {
     console.log(`I am ${name}`);
     this.next();//调用this.next()才能实现链式
    }
    this.task.push(fn);//因为要队列完成后才执行的next,所以将next放到setTimeout中,就可以执行完script代码后,在执行下一个宏任务的时候执行this.next()
    setTimeout(() => {
      this.next();
    }, 0)
    return this;
}
lyc.prototype.restFirst = function (timer) {
  let fn = () => {
    setTimeout(() => {
      console.log(`Start learning after ${timer} seconds`);
      this.next();
    }, timer * 1000)
  }
  this.task.unshift(fn);
  return this;
}

lyc.prototype.rest = function (timer) {
  let fn = () => {
    setTimeout(() => {
      console.log(`Start learning after ${timer} seconds`);
      this.next();
    }, timer * 1000)
  }
  this.task.push(fn);
  return this;
}
lyc.prototype.learn = function (nationality) {
  let fn = () => {
    console.log(`Learning ${nationality}`);
    this.next();
  }
  this.task.push(fn);
  return this;
}
lyc.prototype.next = function () {
//每次都抽出队头执行
  let fn = this.task.shift();
  //如果存在就执行
  fn && fn()
}

// console.log(new lyc("john"))
// console.log(new lyc("john").rest(2).learn("chinese"))
// console.log(new lyc("john").restFirst(2).learn("chinese"))
console 命令行工具 X clear

                    
>
console