const PROMISE_STATUS_PEDDING = 'pedding'
const PROMISE_STATUS_FULFILLED = 'fulfilled'
const PROMISE_STATUE_REJECTED = 'rejected'
class XYPromise {
constructor(executor) {
this.status = PROMISE_STATUS_PEDDING
this.vaule = undefined
this.reason = undefined
const resolve = (value) => {
if (this.status === PROMISE_STATUS_PEDDING) {
this.status = PROMISE_STATUS_FULFILLED
this.value = value
console.log("resolev被调用")
}
}
const reject = (reason) => {
if (this.status === PROMISE_STATUS_PEDDING) {
this.status = PROMISE_STATUE_REJECTED
this.reason = reason
console.log("reject被调用")
}
}
executor(resolve, reject)
}
}
const promise = new XYPromise((resolve, reject) => {
console.log('pedding')
resolve("resolve message")
// reject("reject message")
})
手写promise-状态设计
最新推荐文章于 2022-08-27 21:41:40 发布