首页 > 其他分享 >LazyMan

LazyMan

时间:2023-02-02 17:44:46浏览次数:47  
标签:tasks console name next delay task 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
    }
}

  

标签:tasks,console,name,next,delay,task,LazyMan
From: https://www.cnblogs.com/zhenjianyu/p/17086815.html

相关文章