LazyMan

class LazyMan {
    tasks = []
    constructor(name){
        this.name = name
        console.log(`I am ${name}`)
        setTimeout(() => {
            this.next()
        })
    }
    next(){
        const task = this.tasks.shift()
        if(task) task()
    }
    do(sth){
        const task = () => {
            console.log(`do ${sth}`)
            this.next()
        }
        this.tasks.push(task)
        return this
    }
    sleep(delay){
        const task = () => {
            setTimeout(() => {
                console.log(`sleep ${delay}s`)
                this.next()
            }, delay * 1000)
        }
        this.tasks.push(task)
        return this
    }
}

  

posted @ 2023-02-02 17:37  671_MrSix  阅读(6)  评论(0编辑  收藏  举报