class HardMan {constructor(name) {this.name = name;this.taskQueue = Promise.resolve();//考察执行栈和事件循环 setTimeout(() => {this.execute();})}execute() {if (this._restFirst) {this.realRest(this._restFirst);this._restFirst = 0;}this.outName();if (this._rest) {this.realRest(this._rest)}if (this._learn) {this.taskQueue = this.taskQueue.then(() => {console.log(`Learning ${this._learn}`)});}}outName() {this.taskQueue = this.taskQueue.then(() => {console.log(`I am ${this.name}`)});return this;}rest(seconds) {this._rest = seconds;return this;}realRest(seconds) {this.taskQueue = this.taskQueue.then(() => { return new Promise(resolve => {setTimeout(() => {console.log(`After rest ${seconds} seconds`);resolve();}, seconds * 10);});});return this;}restFirst(seconds) {this._restFirst = seconds;return this;}learn(subject) {this._learn = subject;return this;}}

牛客热帖

牛客网
牛客企业服务