function promise(constructor) {
this.status = "pending";
this.value = undefined;
this.reason = undefined;
this.resolveArray = [];
this.rejectArray = [];
self = this;
function resolve(value) {
if(this.status === "pending") {
self.value = value;
self.status = "resolved";
self.resolveArray.forEach(item => {
item(self.vlaue);
})
}
};
function reject(reason) {
if(this.status === "pending") {
self.reason = reason;
self.status = "rejected";
self.rejectArray.forEach(item => {
item(self.reason)
})
}
};
try{
constructor(resolve, reject);
}catch(error) {
console.log(error);
}
}
promise.prototype.then = function(onFulfilledill, onRejected) {
onFulfilledill = typeof onFulfilledill==="function" ? onFulfilledill : value => value;
onRejected = typeof onRejected ==="function" ? onRejected : error => {throw error};
let pro = new promise((resolve, reject) =>{
if(this.status === "pending") {
this.resolveArray.push(onFulfilledill(this.value));
this.rejectArray.push(onRejected(this.value));
}
if(this.status === "resolved") {
onFulfilledill(this.value);
}
if(this.status === "rejected") {
onRejected(this.reason);
}
})
return pro;
}
console