我正在查看https://www.promisejs.org/implementing/中的简单承诺的实现我在这里得到了大多数东西...但是我不确定为什么这个特殊的例子需要将处理程序存储为数组。如果仅通过以下代码在状态为PENDING时才推送数组function handle(handler) { if (state === PENDING) { handlers.push(handler); } else { if (state === FULFILLED && typeof handler.onFulfilled === 'function') { handler.onFulfilled(value); } if (state === REJECTED && typeof handler.onRejected === 'function') { handler.onRejected(value); } }}但是,如果我们在调用状态之前通过以下方式立即实现并更改状态=!PENDING,function fulfill(result) { state = FULFILLED; value = result; handlers.forEach(handle); handlers = null;}function reject(error) { state = REJECTED; value = error; handlers.forEach(handle); handlers = null;}即使完成被称为非常规this.done = function (onFulfilled, onRejected) { // ensure we are always asynchronous setTimeout(function () { handle({ onFulfilled: onFulfilled, onRejected: onRejected }); }, 0);}我看不到处理程序将如何具有多个元素,因为无论何时处理然后再处理是否在其中处理另一个进程,我们都会始终观察新的promise对象。即在什么条件下,我们将要求在同一诺言下存储多个成功和失败处理程序?我没看到。请帮助我理解。
添加回答
举报
0/150
提交
取消