What is the intent behind clause 2.2.4 of the Promise / A + specification?

Promise clause 2.2.4 / a + spec says:

onFulfilled or onRejected should not be called until the context stack contains only platform code.

The notes then say that:

Here, "platform code" means an engine, an environment, and a promise of implementation code. In practice, this requirement ensures that onFulfilled and onRejected are executed asynchronously after the loop turn event, which is then invoked, and with a fresh stack.

Is this the intention to ensure that when a chain has a large number of onFulfilled functions, their execution does not block the flow?

Or is there something else between the lines that I am not reading?

+7
javascript promise es6-promise event-loop
source share
1 answer

The reason is that with callbacks it is always asynchronous instead of possibly asynchronous, this gives a more consistent and reliable api to use. Consider the following code

var pizza; browseStackOverflow().then(function(){ eatPizza(pizza); }); pizza = yesterdaysLeftovers; 

Now this snippet clearly suggests that onFulfilled will not be called immediately, and if it weren’t, we would soon have unused pizza and we would remain hungry. Although the error would be easy to fix in this case, the execution order is easier to track, and thus the api is easier to use when you can make some assumptions like this.

There is a problem with the Promises / A + GitHub repo with a discussion on this.

+8
source share

All Articles