function MyPromise(func){
this.status = "pending";
this.rightResult = null;
this.errorResult = null;
this.errorFunction = true;
this.out = false;
let self = this
function resolve(value){
if(self.out) return
self.status = 'resolved'
self.rightResult = value
self.out = true
};
function reject(value){
if(self.out) return
self.status = 'rejected'
self.errorResult = value
self.out = true
};
try{
func(resolve,reject)
}catch(e){
console.log(e)
}
}
MyPromise.prototype.then = function(onResolved,onRejected){
if (typeof onRejected !== 'function'){
this.errorFunction = false
}
if(this.status === 'resolved' && typeof onResolved === 'function'){
onResolved(this.rightResult)
}
else if(this.status === 'rejected' && typeof onResolved === 'function'){
onRejected(this.errorResult)
}
}
MyPromise.prototype.catch = function(onRejected){
if(this.status = 'rejected' && !this.errorFunction){
onRejected(this.errorResult)
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。