const PENDING = 'pending'
const FULFILLED = 'fulfilled'
const REJECTED = 'rejected'
class VPromise {
constructor(excutor) {
this.status = PENDING
this.value = undefined
try {
excutor(this._resolve, this._reject)
} catch (e) {
this._reject(e)
}
}
_resolve = value => {
if (this.status === PENDING) {
this.status = FULFILLED
this.value = value
}
}
_reject = reason => {
if (this.status === PENDING) {
this.status = REJECTED
this.value = reason
}
}
then(onFulfilled, onRejected) {
const { status, value } = this
if (status === FULFILLED) {
onFulfilled(value)
}
if (status === REJECTED) {
onRejected(value)
}
}
}
new VPromise((resolve, reject) => {
say('hello world!')
}).then(res => {
console.log(1, res)
}, err => {
console.log(2, err)
})
console