class Promise {
constructor(executor) {
this.value = undefined;
this.reason = undefined;
}
}
function Promise(){}
Promise.prototype.then=function(){}
[](()要求
[](()1. Promise 的状态
当前Promise的状态只能是下面三种状态中的一种,不能存在多种状态。
示例:
const STATUS_PENDING = “pending”;
const STATUS_FULFILLED = “fulfilled”;
const STATUS_REJECTED = “rejected”;
class Promise {
constructor(executor) {
this.status = STATUS_PENDING;
}
}
[](()1.1. 等待(Pending)
处于等待时,状态可以迁移至执行态或拒绝态
[](()1.2. 执行(Fulfilled)
处于执行时:
-
不能迁移至其他任何状态
-
必须拥有一个不可变的终值
[](()1.3. 拒绝(Rejected)
处于拒绝时:
-
不能迁移至其他任何状态
-
必须拥有一个不可变的reason
这里的不可变指的是恒等(即可用 ===
判断相等),引用数据类型只要保证地址相等即可。
new Promise((resolve, reject) => {
resolve(‘成功’);
reject(‘拒绝’)
}).then((res) => {