手动实现一个简易版的Promise
function PromiseSimple (excutor) {
let _this = this
let _status = ‘pending‘
let successCallBack = undefined
let failCallBack = undefined
excutor(resolve.bind(_this), reject.bind(_this))
function resovle (params) {
if (_this._status === ‘pending‘) {
_this._status = ‘full‘
_this.successCallBack(params)
}
}
function reject (params) {
if (_this._status === ‘pending‘) {
_this._status = ‘fail‘
_this.failCallBack(params)
}
}
}
PromiseSimple.prototype.then = function (full, fail) {
this.successCallBack = full
this.failCallBack = fail
}