执行 then 方法的时候里面有两个函数参数,我们需要一个判断来确定该执行哪一个参数函数
因为 then 方法是 Promise实例对象 执行的, p.then() 所以 then 方法里的 this 指向的是 p,我们就可以通过 this.PromiseState 状态属性来判断是调用哪一个
并且调用 then 方法的参数函数的时候,这个参数函数也是有一个参数的,这个参数就是实例对象的 PromiseResult,我们也要传过去
Promise.prototype.then = function (onResolved, onRejected) {
if (this.PromiseState === "resolved") {
onResolved(this.PromiseResult);
}
if (this.PromiseState === "rejected") {
onRejected(this.PromiseResult);
}
};