- 可能是异步的,隔了一段时间才改变promise的状态
- 一个promise的实例p,可能会有多个then。 eg: p.then... p.then... p.then...,每个then都要能收到状态变化后的回调
const PENDING = "pending";
const FULFILLED = "fulfilled";
const REJECTED = "rejected";
class FXPromise {
state = PENDING;
result = undefined;
#handlers = [];
constructor(fn) {
const resolveFunc = (res) => {
if (this.state === PENDING) {
this.result = res;
this.state = FULFILLED;
this.#handlers.forEach(({ onFulfilled }) =>
onFulfilled(this.result)
);
}
};
const rejectFunc = (res) => {
if (this.state === PENDING) {
this.result = res;
this.state = REJECTED;
this.#handlers.forEach(({ onRejected }) => onRejected(this.result));
}
};
fn(resolveFunc, rejectFunc);
}
then(onFulfilled, onRejected) {
onFulfilled =
typeof onFulfilled === "function" ? onFulfilled : (x) => x;
onRejected =
typeof onRejected === "function"
? onRejected
: (x) => {
throw x;
};
if (this.state === FULFILLED) {
onFulfilled(this.result);
} else if (this.state === REJECTED) {
onRejected(this.result);
} else if (this.state === PENDING) {
this.#handlers.push({ onFulfilled, onRejected });
}
}
}
const p = new FXPromise((resolve, reject) => {
setTimeout(() => {
reject("err");
resolve(111);
}, 1000);
});
p.then(
(res) => {
console.log("then res ", res);
},
(err) => {
console.log("then err ", err);
}
);
p.then(
(res) => {
console.log("then2 res ", res);
},
(err) => {
console.log("then2 err ", err);
}
);
