手写promise,还是要懂得promise的使用,然后第二版在第一版上进行了部分优化,以及实现了catch和finally方法。
/**
*
* 手写promise
* @class MaoPromise
*/
class MaoPromise {
/**
* 正在执行的状态
*
* @static
* @memberof MaoPromise
*/
static _PROMISE_STATUS_PENDING = "pending";
/**
* 成功执行的状态
*
* @static
* @memberof MaoPromise
*/
static _PROMISE_STATUS_FULFILLED = "fulfilled";
/**
* 失败执行的状态
*
* @static
* @memberof MaoPromise
*/
static _PROMISE_STATUS_REJECTED = "rejected";
/**
* 默认的状态 执行中
*
* @memberof MaoPromise
*/
_status = MaoPromise._PROMISE_STATUS_PENDING;
/**
* 成功执行时 传给 resolve函数的参数
*
* @memberof MaoPromise
*/
_value = undefined;
/**
* 失败执行时 传给 reject函数的参数
*
* @memberof MaoPromise
*/
_reason = undefined;
/**
* 成功执行的回调函数
*
* @memberof MaoPromise
*/
_onFulfilledCallback = [];
/**
* 失败执行的回调函数
*
* @memberof MaoPromise
*/
_onRejectedCallback = [];
/**
* Creates an instance of MaoPromise.
* @param {Function} executor 执行器
* @memberof MaoPromise
*/
constructor(executor) {
try {
executor(this.resolve, this.reject);
} catch (err) {
this.reject(err);
}
}
/**
* 成功时执行
*
* @param {*} value
* @memberof MaoPromise
*/
resolve = (value) => {
if (this._status === MaoPromise._PROMISE_STATUS_PENDING) {
// 延迟执行 queueMicrotask函数 将回调函数的内容加入到微任务中执行
queueMicrotask(() => {
if (this._status !== MaoPromise._PROMISE_STATUS_PENDING) return;
this._value = value;
this._status = MaoPromise._PROMISE_STATUS_FULFILLED;
// 执行成功回调
this._onFulfilledCallback.forEach(callback => {
callback(this._value);
});
});
}
}
/**
* 失败时执行
*
* @param {*} reason
* @memberof MaoPromise
*/
reject = (reason) => {
if (this._status === MaoPromise._PROMISE_STATUS_PENDING) {
queueMicrotask(() => {
if (this._status !== MaoPromise._PROMISE_STATUS_PENDING) return;
this._reason = reason;
this._status = MaoPromise._PROMISE_STATUS_REJECTED;
// 执行失败回调
this._onRejectedCallback.forEach(callback => {
callback(this._reason);
});
});
}
}
/**
* then方法
*
* @param {*} onFulfilled 成功回调
* @param {*} onRejected 失败回调
* @memberof MaoPromise
*/
then(onFulfilled, onRejected) {
// 如果 onRejected函数没有传 想要在catch方法中传回调
// TODO 那么如果传入了onRejected回调,又使用catch进行捕获会如何?
onRejected = onRejected ?? (err => { throw err });
// 如果第一个then的resolve函数有返回值,且链式调用过程后面出现的是catch
// 则成功回调函数是 undefined,也就是返回值不会被处理
// 所以我们需要在调用catch的时候,将上一个resolve的结果返回出去
// 如果不给 onFulfilled赋值,则catch后面链式调用里面的回调函数都不会执行
onFulfilled = onFulfilled ?? (value => value);
return new MaoPromise((resolve, reject) => {
// TODO 执行then函数的时候,状态已经确定了,则直接执行成功回调函数
if (this._status === MaoPromise._PROMISE_STATUS_FULFILLED) {
if (typeof onFulfilled === "function") {
this._executorFunctionWithCatchError(onFulfilled, this._value, resolve, reject);
}
}
else if (this._status === MaoPromise._PROMISE_STATUS_REJECTED) {
if (typeof onRejected === "function") {
this._executorFunctionWithCatchError(onRejected, this._reason, resolve, reject);
}
} else { // pending 状态
// TODO 副作用函数的返回值 作为then函数返回值promise的(resolve,reject)的参数
// 状态还没确定之前 搜集副作用 在状态改变之后 一起执行
if (typeof onFulfilled === "function")
// 为了收集到副作用执行后的返回值 我们将副作用函数放到新的函数中 然后加入到副作用数组中
this._onFulfilledCallback.push(() => {
this._executorFunctionWithCatchError(onFulfilled, this._value, resolve, reject);
});
if (typeof onRejected === "function")
this._onRejectedCallback.push(() => {
this._executorFunctionWithCatchError(onRejected, this._reason, resolve, reject);
});
}
});
}
/**
* catch方法的设计 巧妙的用了then方法,
* 但是考虑到我们可能会在catch方法后面,
* 链式的调用finally方法,所以需要将调用的then方法的返回值 继续返回
*
* @param {*} onRejected 失败/异常处理回调
* @memberof MaoPromise
*/
catch(onRejected) {
return this.then(undefined, onRejected);
}
/**
* 最终执行promise的善后工作的代码
*
* @param {*} onFinally 最终回调
* @memberof MaoPromise
*/
finally(onFinally) {
// 还是借用then方法,不管成功还是失败/异常 都会执行最终回调
if (typeof onFinally === "function")
this.then(() => {
onFinally();
}, () => {
onFinally();
});
}
/**
* 执行副作用函数 进行异常的捕获处理
*
* @param {*} execFn 副作用函数
* @param {*} value 上一个回调函数(resolve,reject)执行时传入的参数
* @param {*} resolve 成功回调
* @param {*} reject 失败回调
* @memberof MaoPromise
*/
_executorFunctionWithCatchError(execFn, value, resolve, reject) {
try {
const res = execFn(value);
resolve(res);
} catch (err) {
reject(err);
}
}
}
const promise = new MaoPromise((resolve, reject) => {
resolve("111");
// reject("11211");
// throw new Error("222");
});
promise.then((res) => {
console.log(res);
return "sss";
}, err => {
console.log(err);
}).catch(res => {
console.log(res);
}).finally(() => {
console.log("finally");
})