const PENDING = "pending"
const FULLFILLED = "fullfilled"
const REJECTED = "rejected"
class NumoPromise {
constructor(exector) {
if (typeof exector !== 'function') throw Error('exector params must be a function')
this.status = PENDING
this.value = undefined
this.reason = undefined
const resolve = (value) => {
if (this.status === 'pending') {
queueMicrotask(() => {
if (this.status !== PENDING) return
this.status = FULLFILLED
this.value = value
console.log(value)
})
}
}
const reject = (reason) => {
if (this.status === 'pending') {
queueMicrotask(() => {
if (this.status !== PENDING) return
this.status = REJECTED
this.reason = reason
console.log(reason)
})
}
}
exector(resolve, reject)
}
}
/**
* 解决1: 让reject和resolve执行是微任务
* 解决2: 一旦执行了resolve,那么后面的reject就无法执行
*/
const np = new NumoPromise((resolve, reject) => {
reject('error')
resolve(1)
})
console.log('先执行')