let STATE = {
进行中: 'pending',
成功: 'fulfilled',
失败: 'rejected',
}
function Mypromise(fn) {
this.state = STATE['进行中']; // 状态
this.value = null // 成功的值
this.error = null // 失败的值
let onFulfilled = function (value) { // 成功回调函数
console.log(value)
}
let onRejected = function (value) { // 失败回调函数
console.log(value)
}
let resolve = (value) => { // 执行成功的函数
if (this.state === STATE['进行中']) {
onFulfilled(value)
this.state = STATE['成功']
}
}
let reject = (value) => { // 执行失败的函数
if (this.state === STATE['进行中']) {
onRejected(error)
this.state = STATE['失败']
}
}
fn(resolve, reject)
}
Mypromise.prototype.then = function (onFulfilled, onRejected) {
console.log(this)
}
let p1 = new Mypromise((resolve, reject) => {
// console.log(111)
resolve(222)
reject(333)
})
p1.then()
console