Nodejs 的 Promise 实现是不是有问题
1个回答
展开全部
Promise.reject的流程大概是这样的:
1. Let C be the this value.
2. Let promiseCapability be NewPromiseCapability(C).
3. ReturnIfAbrupt(promiseCapability).
4. Let rejectResult be the result of calling the [[Call]] internal method of promiseCapability.[[Reject]] with undefined as thisArgument and (r) as argumentsList.
5. ReturnIfAbrupt(rejectResult).
6. Return promiseCapability.[[Promise]].
1-3都是同步的操作,无非就是建一个叫PromiseCapability的东西出来,重点在第4步
第4步说的是调用PromiseCapability上一个叫[[Reject]]的方法,这个方法是什么,可以从规范里找出来。规范写得也是略复杂,总之找来找去,这个方法对应的是PromiseCapability Records这个东西上的[[Reject]]属性,它的解释是“The function that is used to reject the given promise object.”
然后继续往下找,可以在《Promise Reject Functions》这一节找到这个方法的说明:
1. Assert: F has a [[Promise]] internal slot whose value is an Object.
2. Let promise be the value of F's [[Promise]] internal slot.
3. Let alreadyResolved be the value of F's [[AlreadyResolved]] internal slot.
4. If alreadyResolved.[[value]] is true, then return undefined.
5. Set alreadyResolved.[[value]] to true.
6. Return RejectPromise(promise, reason).
上面这6步的重点显然在第6点,RejectPromise这个动作的说明是:
1. Assert: the value of promise's [[PromiseState]] internal slot is "pending".
2. Let reactions be the value of promise's [[PromiseRejectReactions]] internal slot.
3. Set the value of promise's [[PromiseResult]] internal slot to reason.
4. Set the value of promise's [[PromiseFulfillReactions]] internal slot to undefined.
5. Set the value of promise's [[PromiseRejectReactions]] internal slot to undefined.
6. Set the value of promise's [[PromiseState]] internal slot to "rejected".
7. Return TriggerPromiseReactions(reactions, reason).
重点在第7步,TriggerPromiseReactions:
1. Repeat for each reaction in reactions, in original insertion order
1.1. Perform EnqueueTask("PromiseTasks", PromiseReactionTask, (reaction, argument)).
2. Return undefined.
这里看到EnqueueTask就不用往下看了,这个操作是异步的,往任务队列里放一个任务来调用then/catch挂上来的回调
上面这个过程,从RejectPromise开始和new Promise(executor)是一模一样的,所以说白了new Promise和Promise.reject其实一样,到TriggerPromiseReactions为止挂上异步任务
所以new Promise(executor)里直接resolve和Promise.resolve是一个概念,都是“当前Promise的状态变了,所有回调加到任务队列”,那后面到底哪个会影响p4就完全看遍历顺序了。
1. Let C be the this value.
2. Let promiseCapability be NewPromiseCapability(C).
3. ReturnIfAbrupt(promiseCapability).
4. Let rejectResult be the result of calling the [[Call]] internal method of promiseCapability.[[Reject]] with undefined as thisArgument and (r) as argumentsList.
5. ReturnIfAbrupt(rejectResult).
6. Return promiseCapability.[[Promise]].
1-3都是同步的操作,无非就是建一个叫PromiseCapability的东西出来,重点在第4步
第4步说的是调用PromiseCapability上一个叫[[Reject]]的方法,这个方法是什么,可以从规范里找出来。规范写得也是略复杂,总之找来找去,这个方法对应的是PromiseCapability Records这个东西上的[[Reject]]属性,它的解释是“The function that is used to reject the given promise object.”
然后继续往下找,可以在《Promise Reject Functions》这一节找到这个方法的说明:
1. Assert: F has a [[Promise]] internal slot whose value is an Object.
2. Let promise be the value of F's [[Promise]] internal slot.
3. Let alreadyResolved be the value of F's [[AlreadyResolved]] internal slot.
4. If alreadyResolved.[[value]] is true, then return undefined.
5. Set alreadyResolved.[[value]] to true.
6. Return RejectPromise(promise, reason).
上面这6步的重点显然在第6点,RejectPromise这个动作的说明是:
1. Assert: the value of promise's [[PromiseState]] internal slot is "pending".
2. Let reactions be the value of promise's [[PromiseRejectReactions]] internal slot.
3. Set the value of promise's [[PromiseResult]] internal slot to reason.
4. Set the value of promise's [[PromiseFulfillReactions]] internal slot to undefined.
5. Set the value of promise's [[PromiseRejectReactions]] internal slot to undefined.
6. Set the value of promise's [[PromiseState]] internal slot to "rejected".
7. Return TriggerPromiseReactions(reactions, reason).
重点在第7步,TriggerPromiseReactions:
1. Repeat for each reaction in reactions, in original insertion order
1.1. Perform EnqueueTask("PromiseTasks", PromiseReactionTask, (reaction, argument)).
2. Return undefined.
这里看到EnqueueTask就不用往下看了,这个操作是异步的,往任务队列里放一个任务来调用then/catch挂上来的回调
上面这个过程,从RejectPromise开始和new Promise(executor)是一模一样的,所以说白了new Promise和Promise.reject其实一样,到TriggerPromiseReactions为止挂上异步任务
所以new Promise(executor)里直接resolve和Promise.resolve是一个概念,都是“当前Promise的状态变了,所有回调加到任务队列”,那后面到底哪个会影响p4就完全看遍历顺序了。
微测检测5.10
2023-07-11 广告
2023-07-11 广告
IEC62133与en62133的区别如下:1. 认证机构不同:IEC62133是国际的标准,它以国际通用的标准进行生产;而en62133采用的是欧盟的标准,它使用欧盟的生产需求进行生产。2. 宗旨不同:IEC62133的宗旨是促进电气、电...
点击进入详情页
本回答由微测检测5.10提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询