class MyPromise{
constructor(fn){
this.state = 'pending';
this.value = '';
this.resolvedCallbacks = [];
this.rejectedCallbacks = [];
fn(this.resolve.bind(this),this.reject.bind(this))
}
resolve(value){
if(this.state === 'pending'){
this.state = 'fulfilled';
this.value = value;
this.resolvedCallbacks.map(cb => cb(value))
}
}
reject(value){
if(this.state === 'pending'){
this.state = 'rejected';
this.value = value;
this.rejectedCallbacks.map(cb => cb(value))
}
}
then(onResolved,onRejected){
if(this.state === 'fulfilled'){
onResolved(this.value)
}
if(this.state === 'rejected'){
onRejected(this.value)
}
if(this.state === 'pending'){
this.resolvedCallbacks.push(onResolved);
this.rejectedCallbacks.push(onRejected);
}
}
}