function MyPromise(resolver) {
this._status = 'PENDDING'
this._value = undefined
this._resolveCallback = undefined
this._rejectCallback = undefined
resolver(resolve, reject)
function resolve(val) {
setTimeout(function () {
console.log(88)
this._resolveCallback && this._resolveCallback(val)
})
}
function reject(val) {
setTimeout(function () {
this._rejectCallback && this._rejectCallback(val)
})
}
}
MyPromise.prototype.then = function (onResolve, onReject) {
this._resolveCallback = onResolve
this._rejectCallback = onReject
}
var fn=function(resolve, reject){
console.log('begin to execute!');
setTimeout(function () {
var number=Math.random();
if(number<=0.5){
resolve('less than 0.5');
}else{
reject('greater than 0.5');
}
}, 200)
}
var p = new MyPromise(fn);
p.then(function(data){
console.log('resolve: ', data);
}, function(data){
console.log('reject: ', data);
})
console