//promise用法
/*
var p = new Promise(function (resolve, reject) {
setTimeout(function () {
// A动画
console.log('A');
resolve();
}, 300);
});
p.then(function () {
setTimeout(function () {
// B动画
console.log('B');
}, 300);
});
*/
//实现promise
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;
}
//更新state为RESOLVED,并且执行成功处理队列
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)
}
//更新state为REJECTED,并且执行失败处理队列
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');
// reject('you are err');
}).done((res) => {
console.log(res);
}).fail((res) => {
console.log('error');
})
console