class PromiseNew{
constructor(fn){
this.state = 'PENDING';
this.doneList = [];
this.failList = [];
fn(this.resolve.bind(this),this.reject.bind(this));
}
done(handle) {
if(typeof handle === 'function'){
this.doneList.push(handle);
}else{
throw new Error('缺少回调函数');
}
return this;
}
fail(handle) {
if(typeof handle === 'function') {
this.failList.push(handle);
}else {
throw new Error('缺少回调函数');
}
return this;
}
then(success,fail) {
this.done(success || function () { }).fail(fail || function(){ });
return this;
}
resolve() {
this.state = 'RESOLVED';
let args = Array.prototype.slice.call(arguments);
setTimeout(function () {
this.doneList.forEach((item,key,arr)=>{
item.apply(null,args);
arr.shift();
});
}.bind(this),200)
}
reject() {
this.state = 'REJECTED';
let args = Array.prototype.slice.call(arguments);
setTimeout(function(){
this.failList.forEach((item,key,arr)=>{
item.apply(null,args);
arr.shift();
});
}.bind(this),200)
}
}
new PromiseNew((resolve, reject) => {
resolve('hello world');
}).done((res) => {
console.log(res);
}).fail((res) => {
console.log('error');
})
console