Open ZhengXingchi opened 4 years ago
class LazyManClass {
constructor(name) {
console.log(name)
this.tasks = []
setTimeout(() => {
this.next()
}, 0)
}
sleep(time) {
this.tasks.push(() => {
setTimeout(() => {
console.log(time)
this.next()
}, time * 1000)
})
return this
}
sleepFirst(time) {
this.tasks.unshift(() => {
setTimeout(() => {
console.log(time)
this.next()
}, time * 1000)
})
return this
}
eat(lunch) {
this.tasks.push(() => {
console.log(lunch)
this.next()
})
return this
}
next() {
let fn = this.tasks.shift()
fn && fn()
}
}
function LazyMan(name) {
return new LazyManClass(name)
}
LazyMan('Tony').eat('lunch').eat('dinner').sleepFirst(5).sleep(10).eat('junk food')