面試題-lazyMan實現

原文:如何實現一個LazyManjavascript

面試題目

實現一個LazyMan,能夠按照如下方式調用:java

LazyMan('Hank'),輸出:面試

Hi, This is Hank!異步

LazyMan('Hank').sleep(5).eat('dinner'),輸出:async

Hi, This is Hank!
// 等待5秒
Weak up after 10
Eat dinner ~post

LazyMan('Hank').eat('dinner').eat('supper'),輸出測試

Hi, this is Hank!
Eat dinner ~
Eat supper ~this

LazyMan('Hank').sleepFirst(5).eat('supper'),輸出code

// 等待5秒
Wake up after 5
Hi, this is Hank!
Eat supper對象

以此類推

題目解析

  • 須要封裝一個對象,而且這個對象提供不一樣的方法,好比eat
  • 能進行鏈式調用,那麼每一個調用方法,都必須返回當前對象
  • sleepsleepFirst方法須要時異步的

解題思路

  • 採用 ES6 的 class,來實現,封裝對象_LazyMan
  • 提供一系列方法,好比eatsleepsleepFirst異步方法採用PromisesetTimeout實現
  • 鏈式調用,考慮到其中含異步方法,採用任務隊列及 ES6 的async wait實現。每次調用都往隊列中加入方法,而後循環調用任務隊列,而循環中經過異步實現異步的方法,保證正確。

具體實現

class _LazyMan {
  constructor (name) {
    this.taskQueue = [];
    this.runTimer = null;
    this.sayHi(name);
  }

  run () {
    if (this.runTimer) {
      clearTimeout(this.runTimer);
    }

    this.runTimer = setTimeout(async () => {
      for (let asyncFun of this.taskQueue) {
        await asyncFun()
      }
      this.taskQueue.length = 0;
      this.runTimer = null;
    })
    return this;
  }

  sayHi (name) {
    this.taskQueue.push(async () => console.log(`Hi, this is ${name}`));
    return this.run();
  }

  eat (food) {
    this.taskQueue.push(async () => console.log(`Eat ${food}`));
    return this.run();
  }

  sleep (second) {
    this.taskQueue.push(async () => {
      console.log(`Sleep ${second} s`)
      return this._timeout(second)
    });
    return this.run();
  }

  sleepFirst (second) {
    this.taskQueue.unshift(async () => {
      console.log(`Sleep first ${second} s`)
      return this._timeout(second);
    });
    return this.run();
  }

  async _timeout (second) {
    await new Promise(resolve => {
      setTimeout(resolve, second * 1e3);
    })
  }
}

// 測試
var LazyMan = name => new _LazyMan(name)

// lazyMan('Hank');
lazyMan('Hank').sleep(10).eat('dinner');
// lazyMan('Hank').eat('dinner').eat('supper');
// lazyMan('Hank').sleepFirst(5).eat('supper');
相關文章
相關標籤/搜索