class MyPromise {
constructor(callback) {
this.init();
try {
// 防止修改this指向
callback(this.resolve.bind(this, this), this.reject.bind(this, this));
} catch (e) {
this._errinfo = e;
}
}
init () {
// 回调仓库
this._store = [];
// 处理在 new MyPromise((resolve, reject) => {没有异步的情况下调用resolve/reject,或者这里的同步代码报错}),
// 异步操作里面的报错是同步try/catch捕捉不了的。
// 比如 new MyPromise(() => setTimeout(() => undefinedMethod())) 这个定时器里面的报错是不会通过传递到catch回调的
// 除非是这样 new MyPromise(() => undefinedMethod())在同步执行下报错,才会传递给catch回调
this._errinfo = '';
this._succinfo = '';
// a = new MyPromise((resolve, reject) => setTimeout(() => resolve(), 1230)); a.then(() => ...) 异步一下
// 让then/catch/finally都挂载好
this._timer = null;
// 表示是否调用过 resolve 或者 reject 函数
// 避免在 a = new MyPromise((resolve, reject) => setTimeout(() => resolve(), 1230)); a.then(() => ...) 的时候,
// 不会正常调用then/catch或者finally
this._start = false;
}
resolve (ctx, succ) {
ctx._start = true;
// 出现在没有在异步下直接调用resolve
if (ctx._store.length === 0) {
ctx._succinfo = succ;
return;
}
let cb, res;
while (cb = ctx._store.shift()) {
if (cb.type === 'THEN') {
try {
res = cb.callback(res || succ);
// 返回新的my promise实例的时候,将剩余的回调拷贝过去
if (res instanceof MyPromise) {
while(cb = ctx._store.shift()) {res[cb.type.toLowerCase()](cb.callback)}
break;
}
ctx._succinfo = res;
} catch (e) {
ctx.reject(ctx, e);
break;
}
}
if (ctx._handleFinally(cb) === false) {
break;
}
}
}
reject (ctx, err) {
ctx._start = true;
// 出现在没有在异步下直接调用reject
if (ctx._store.length === 0) {
ctx._errinfo = err;
return;
}
let cb, res;
while (cb = ctx._store.shift()) {
if (cb.type === 'CATCH') {
try {
res = cb.callback(res || err);
ctx.resolve(ctx);
break;
} catch (e) {
res = e;
ctx._errinfo = e;
}
}
if (ctx._handleFinally(cb) === false) {
break;
}
}
}
_handleFinally (storeItem) {
if (storeItem.type === "FINALLY") {
try {
storeItem.callback();
} catch (e) {
this.reject(this, e);
return false;
}
}
}
then (callback) {
this._addCallback('THEN', callback);
this._deferHandle(() => {
this.resolve(this, this._succinfo);
this._succinfo = '';
});
return this;
}
catch (callback) {
this._addCallback('CATCH', callback);
this._deferHandle(() => {
this.reject(this, this._errinfo);
this._errinfo = '';
});
return this;
}
finally (callback) {
this._addCallback('FINALLY', callback);
this._deferHandle(() => this.resolve(this));
return this;
}
_deferHandle(callback) {
if (!this._timer && this._start) {
this._timer = setTimeout(() => {
callback();
this._timer = null;
});
}
}
_addCallback (typeName, callback) {
this._store.push({
type: typeName,
callback: callback
});
}
// 参数解构, 有数组解构写法之后,这个就不需要了
// static separate (callback) {
// return (args) => callback.apply(null, args);
// }
static all (pros) {
return new MyPromise((res, rej) => {
let args = [],
book = [],
isCatch = false,
handleResolve, handleCatch;
handleResolve = (d, i) => {
if (!isCatch) {
args[i] = d;
book[i] = 1; // 记录有哪些执行成功了
// 当执行成功的个数和总执行个数相等,执行完毕
if (book.filter(v => v === 1).length === pros.length) {
res(args);
}
}
};
handleCatch = e => {
isCatch = true;
rej(e);
handleCatch = () => {};
};
pros.forEach((item, i) => item.then(d => handleResolve(d, i)).catch(handleCatch));
});
}
static race (pros) {
return new MyPromise((res, rej) => {
let isComplete = false,
isCatch = false,
handleResolve, handleCatch;
handleResolve = d => {
if (!isCatch && !isComplete) {
isComplete = true;
res(d);
}
};
handleCatch = e => {
if (!isComplete) {
isCatch = true;
rej(e);
handleCatch = () => {};
}
};
pros.forEach(item => item.then(handleResolve).catch(handleCatch));
});
}
static reject(msg) {
return new MyPromise((_, reject) => reject(msg));
}
static resolve(msg) {
return new MyPromise(resolve => resolve(msg));
}
}
网友评论