function MayPromise(callback) {
this.status = "pending"
this.value = null
this.successCallback = []
const resolve = (value) => {
if (this.status === "pending") {
this.status = "success"
this.value = value
this.successCallback.map(item => item(value))
}
}
const reject = value => {
if (this.status === "pending") {
this.status === "reject"
}
}
callback(resolve, reject)
}
MayPromise.prototype.then = function (callback) {
if (this.status === "success") {
callback(this.value)
} else if (this.status === "pending") {
this.successCallback.push(callback)
}
}
new MayPromise((resolve) => {
setTimeout(() => {
resolve('123456')
}, 1000)
}).then(res => console.log('res', res))
网友评论