-
Notifications
You must be signed in to change notification settings - Fork 6
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
Labels
question
Further information is requested
Comments
'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) |
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
实现一个Person类,类里有eat、work、sleep三个方法,支持如下调用方式eat().work(10).sleep(5)
打印结果为
eat()
eat()->work(10) //等待10后打印
eat()->work(10)->sleep(5) //等待5秒后打印
The text was updated successfully, but these errors were encountered: