手写promise,将静态方法和实例方法都进行了一遍实现。也可以实现链式调用。
class MaoPromise {
static _PROMISE_STATUS_PENDING = "pending";
static _PROMISE_STATUS_FULFILLED = "fulfilled";
static _PROMISE_STATUS_REJECTED = "rejected";
_status = MaoPromise._PROMISE_STATUS_PENDING;
_value = undefined;
_reason = undefined;
_onFulfilledCallback = [];
_onRejectedCallback = [];
constructor(executor) {
try {
executor(this.resolve, this.reject);
} catch (err) {
this.reject(err);
}
}
static resolve(value) {
return new MaoPromise((resolve) => resolve(value));
}
static reject(reason) {
return new MaoPromise((resolve, reject) => reject(reason));
}
static all(promises) {
return new MaoPromise((resolve, reject) => {
const values = [];
promises.forEach(promise => {
promise.then(res => {
values.push(res);
if (values.length === promises?.length) resolve(values);
}, err => reject(err));
});
});
}
static allSettled(promises) {
return new MaoPromise((resolve) => {
const result = [];
promises.forEach(promise => {
promise.then(res => {
result.push({
status: MaoPromise._PROMISE_STATUS_FULFILLED,
value: res
});
if (result.length === promises?.length) resolve(result);
}, err => {
result.push({
status: MaoPromise._PROMISE_STATUS_REJECTED,
reason: err
});
if (result.length === promises?.length) resolve(result);
});
})
});
}
static race(promises) {
return new MaoPromise((resolve, reject) => {
promises.forEach(promise => {
promise.then(resolve, reject);
});
});
}
static any(promises) {
return new MaoPromise((resolve, reject) => {
const reasons = [];
promises.forEach(promise => {
promise.then(resolve, err => {
reasons.push(err);
if (reasons.length === promises?.length) reject(new AggregateError(reasons));
});
});
});
}
resolve = (value) => {
if (this._status === MaoPromise._PROMISE_STATUS_PENDING) {
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);
});
});
}
}
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(onFulfilled, onRejected) {
onRejected = onRejected ?? (err => { throw err });
onFulfilled = onFulfilled ?? (value => value);
return new MaoPromise((resolve, reject) => {
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 {
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(onRejected) {
return this.then(undefined, onRejected);
}
finally(onFinally) {
if (typeof onFinally === "function")
this.then(() => {
onFinally();
}, () => {
onFinally();
});
}
_executorFunctionWithCatchError(execFn, value, resolve, reject) {
try {
const res = execFn(value);
resolve(res);
} catch (err) {
reject(err);
}
}
}
const p1 = new MaoPromise((resolve, reject) => {
setTimeout(() => {
resolve(111);
}, 1000);
});
const p2 = new MaoPromise((resolve, reject) => {
setTimeout(() => {
reject(222);
}, 2000);
});
const p3 = new MaoPromise((resolve, reject) => {
setTimeout(() => {
resolve(333);
}, 3000);
});
MaoPromise.any([p2]).then((res) => {
console.log(res);
}).catch(err => {
console.log(err.errors);
})