To implement a class Lazing function, the output should be written as follows.

Lazing("Garry")
//  "hello Garray"

Lazing("Garry").sleep(10).eat("rice")
//  "hello Garray"
// 10...
//  "eating rice"

Lazing("Garry").eat("rice").eat("bread")
//  "eating rice"
//  "eating bread"

Lazing("Garry").sleepFirst(5).eat("rice")
// 5...
//  "hello Garray"
//  "eating rice"
< hr >

the difficulty is how to deal with timers? If the sleepFirst timer is post-set, how to implement it?

Lazing("Garry").eat("rice").sleepFirst(5)
// 5...
//  "hello Garray"
//  "eating rice"

A rough demo, timer is left behind. I can't figure it out for a moment. This demo is executed in sequence

.
var Lazing2 = function(name){
    return {
        _name : name,
        _food : [],
        _timeLimit : 0,
        eat : function(food){
            var _this = this;
            setTimeout(function(){
                console.log(_this._name,'',food)
            },_this._timeLimit*1000);
            return _this
        },
        delay : function(time){
            var _this = this;
            _this._timeLimit = time;
            return _this;
        }
    }
}

Lazing2("").eat('').delay(3).eat('');

the general idea is to construct a task queue

class Lazing {
  constructor(item = '') {
    this.queue = [{
        key: 'init',
        val() {
          console.log('hello ' + item)
        }
      }]
  }

  eat(item) {
    this.queue.push({
      key: 'eat',
      val() {
        console.log('eating ' + item)
      }
    })
    return this
  }

  sleep(time) {
    this.queue.push({
      key: 'sleep',
      val: time * 1000
    })
    return this
  }

  sleepFirst(time) {
    this.queue.unshift({
      key: 'sleep',
      val: time * 1000
    })
    return this
  }

  exec() {
    for (let i = 0; i < this.queue.length; iPP) {
      let key = this.queue[i]['key']
      let val = this.queue[i]['val']
      if (key === 'sleep') {
        this.queue.shift()
        setTimeout(this.exec.bind(this), val)
        break
      } else {
        val()
        this.queue.shift()
        i--
      }
    }
  }
}

but the method of calling is slightly different, but it can achieve the effect

new Lazing('Garry').exec()

new Lazing('Garry').sleep(3).eat('rice').exec()

new Lazing('Garry').eat('rice').eat('bread').exec()

new Lazing('Garry').sleepFirst(3).eat('rice').exec()

new Lazing('Garry').eat('rice').sleepFirst(3).exec()

class _Lazing{
        constructor(str){
            this.promise=new Promise(res=>this.res=res);
            this.promise=this.promise.then(()=>console.log(`hello ${str}`));
            this.t=setTimeout(()=>this.res(),0);
        }
        sleep(time){
            this.promise=this.promise.then(()=>new Promise(res=>setTimeout(res,time*1000)));
            return this;
        }
        eat(something){
            this.promise=this.promise.then(data=>console.log(`eating ${something}`));
            return this;
        }
        sleepFirst(time){
            clearTimeout(this.t);
            new Promise(res=>setTimeout(res,time*1000)).then(()=>this.res());
            return this;
        }
    }
    const Lazing=str=>new _Lazing(str);

//    Lazing('Garry')
    //  'hello Garray'

//    Lazing('Garry').sleep(10).eat('rice')
    //  'hello Garray'
    // 10...
    //  'eating rice'

    Lazing('Garry').eat('rice').eat('bread')
    //  'eating rice'
    //  'eating bread'

//    Lazing('Garry').sleepFirst(5).eat('rice')
    // 5...
    //  'hello Garray'
    //  'eating rice'

it seems that the construction part can only be executed asynchronously. I haven't thought of any other methods to be a master.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1bf28ac-31e11.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1bf28ac-31e11.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?