Appearance
then的实现
本节目标:理解“then的实现”的核心思路,并能把它用于实际开发或面试表达。
Then方法实际上就是发布订阅模式的on方法,用来添加事件触发时的回调函数;
promise中不同的是有成功回调方法,还有失败回调方法;
then方法的参数分别是成功回调和失败回调,then(onFulfilled,onRejected);
当promise的状态是已经是凝固时,我们直接执行成功回调onFulfilled或失败回调onRejected;
then(onFulFilled, onRejected) { //状态已经凝固
if (this.status === FULFILLED) { onFulFilled(this.value)}
if (this.status === REJECTED) { onRejected(this.reason)}
//状态未凝固
if (this.status === PENDING) { this.onFulfilledCallbacks.push(() => { onFulFilled(this.value) `})`
this.onRejectedCallbacks.push(() => { onRejected(this.reason) `})`
