const PENDING = 'pending'
const FUFILLED = 'fulfilled'
const REJECTED = 'rejected'
class MyPromise {
constructor (executor) {
executor(this.resolve, this.reject)
}
status = PENDING
value = undefined
reason = undefined
resolve = value => {
if (this.status !== PENDING) return
this.status = FUFILLED
this.value = value
}
reject = reason => {
if (this.status !== PENDING) return
this.status = REJECTED
this.reason = reason
}
then(successCallback, failCallback) {
if (this.status === FUFILLED) {
successCallback(this.value)
} else if (this.status === REJECTED) {
failCallback(this.reason)
}
}
}
const mypromise = new MyPromise((resolve, reject) => {
resolve('successValue')
})
mypromise.then(value => {
console.log(value)
}, reason => {
console.log(reason)
})