Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

10. 一道异步相关的面试题 #10

Open
KRISACHAN opened this issue May 20, 2020 · 2 comments
Open

10. 一道异步相关的面试题 #10

KRISACHAN opened this issue May 20, 2020 · 2 comments
Labels
question Further information is requested

Comments

@KRISACHAN
Copy link
Owner

实现一个Person类,类里有eat、work、sleep三个方法,支持如下调用方式eat().work(10).sleep(5)

打印结果为
eat()
eat()->work(10) //等待10后打印
eat()->work(10)->sleep(5) //等待5秒后打印

@KRISACHAN KRISACHAN added the question Further information is requested label May 20, 2020
@KRISACHAN KRISACHAN changed the title 10. 一道异步逻辑题 10. 一道异步相关的面试题 May 20, 2020
@KRISACHAN
Copy link
Owner Author

'use strict'
class Person {
    #queue = []
    #timeout = 0
    #timer = null
    #start = new Date().getTime()
    constructor() {}
    #run = async () => {
        clearTimeout(this.#timer)
        this.#timer = null
        return await new Promise(resolve => {
            this.#timer = setTimeout(() => {
                console.log(this.#queue)
                console.log(this.#timeout)
                console.log(new Date().getTime() - this.#start)
                resolve()
            }, this.#timeout * 1000)
        })
    }
    eat = food => {
        this.#queue.push(food)
        return this
    }
    sleep = async timeout => {
        this.#timeout = timeout
        await this.#run(timeout)
    }
    work = timeout => {
        this.#timeout = timeout
        this.#run(timeout)
        return this
    }
}

const person1 = new Person()
const person2 = new Person()

person1.eat('呵呵').work(1)
person2.eat('哈哈').work(1).sleep(2)

@WSQ233
Copy link

WSQ233 commented May 21, 2020

class Person {
    constructor() {
        this.deep = []
        this.TimeList = []
        this.Time = 0
    }

    run = async (time, name) => {
        this.TimeList.push(() => {
            return setTimeout(() => {
                this.deep.push(`${name}(${time})`)
                console.log(this.deep.join('->'))
                this.TimeList.shift()
                this.TimeList.length && this.TimeList[0]()
            }, typeof time === "number" ? time : 0);
        })
        !this.Time && (this.Time = this.TimeList[0]())
    }

    eat = value => {
        this.deep.push(`eat(${value})`)
        console.log(this.deep)
        return this
    }
    work = time => {
        this.run(time, 'work')
        return this
    }
    sleep = time => {
        this.run(time, 'sleep')
        return this
    }
}
let p1 = new Person()
console.log(p1)
console.log(p1.eat('233').sleep(5000).work(1000))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants