Promise的结构
Promise实例有三种状态:
pending: 等待态
fulfilled: 成功态
rejected: 失败态
状态流转:
- Promise实例的状态只能从
pending向fulfilled或rejected流转
- 流转之后状态不可变
- 当执行
resolve,Promise实例的状态从pending变成fulfilled
- 当执行
reject,Promise实例的状态从pending变成rejected
解决的问题:executor()执行时候,resolve和reject的this丢失
class MyPromise {
static PENDING = "pending";
static FULFILLED = "fulfilled";
static REJECTED = "rejected";
#status;
#value;
#reason;
constructor(executor) {
this.#status = MyPromise.PENDING;
this.#value = undefined;
this.#reason = undefined;
executor(this.#resolve.bind(this), this.#reject.bind(this));
}
#resolve(value) {
if (this.#status === MyPromise.PENDING) {
this.#status = MyPromise.FULFILLED;
this.#value = value;
}
}
#reject(reason) {
if (this.#status === MyPromise.PENDING) {
this.#status = MyPromise.REJECTED;
this.#reason = reason;
}
}
then(onFulfilled, onRejected) {
if (this.#status === MyPromise.FULFILLED) {
onFulfilled(this.#value);
}
if (this.#status === MyPromise.FULFILLED) {
onRejected(this.#reason);
}
}
}