const PENDDING = "pedding";
const RESOLVED = "resolved";
const REJECTED = "rejected";
function myPromise(fn) {
const that = this;
that.status = PENDDING;
that.value = null;
that.reason = null;
that.resolvedCallBacks = [];
that.rejectedCallBacks = [];
function resolve(value) {
that.status = RESOLVED;
that.value = value;
that.resolvedCallBacks.map(cb=>cb(that.value))
}
function reject(reason){
that.status = REJECTED;
that.reason = reason;
that.rejectedCallBacks.map(cb=>cb(that.reason))
}
try{
fn(resolve,reject)
}catch(e){
reject(e)
}
}
myPromise.prototype.then = function(onFullFilled,onRejected){
const that = this;
if(that.status===PENDDING){
that.resolvedCallBacks.push(onFullFilled);
that.rejectedCallBacks.push(onRejected)
}
if(that.status===RESOLVED){
onFullFilled(that.value)
}
if(that.status===REJECTED){
onRejected(that.reason)
}
return that
}
const p = new myPromise((resolve,reject)=>{
setTimeout(()=>{
resolve('成功')
// reject("失败")
},3000)
})
p.then(res=>{
console.log(res)
},err=>{
console.log(err)
})
console