class Promises{
constructor(fn){
this.state="pending";
fn(this.resolve.bind(this),this.reject.bind(this))
}
resolve(value){
if(this.state!=="pending")return;
this.state="resolve";
let ids=setTimeout(()=>{
this.fn1(value);
clearTimeout(ids);
});
}
reject(value){
if(this.state!=="pending")return;
this.state="reject";
let ids=setTimeout(()=>{
this.fn2(value);
clearTimeout(ids);
});
}
then1(fn1,fn2){
this.fn1=fn1;
this.fn2=fn2;
}
}
let pro=new Promises(function(resolve,reject){
resolve("成功");
reject("失败");
});
pro.then1(function(e){
console.log(e);
},function(d){
console.log(d);
});
手写promise
最新推荐文章于 2023-05-30 08:44:50 发布