-
Notifications
You must be signed in to change notification settings - Fork 164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
What is the intention behind clause 2.2.4 of Promise/A+ spec? #229
Comments
The blocking behavior you describe is certainly one advantage. More generally, it seems to me that the note you quote says the intent clearly enough: it increases deterministic behavior of promises. Handlers will always execute asynchronously, which is easier to reason about than if they sometimes execute synchronously and sometimes don't. |
No, it does not mean that. This might be a behaviour implemented by ES6 Promises and other libraries that use a queue-style asynchronous execution, but it's not required by A+. Btw, this breaks apart anyway when you register handlers after resolution. |
Ah, my bad! I conflated the "handlers must run in order" and "handlers must be called asynchronously" into a single "handlers must run in order in one async step" concept. Of course if each handler is scheduled independently there can be interleaving without breaking P/A+. Thanks! And I should have made it clear I was talking about existing promise chains pre-resolution. :-P |
If I may “jiggle” this thread a bit: What is the benefit of ensuring that handlers be called asynchronously? I’ve got a promise implementation that I’m using to abstract over whether a given library works synchronously or asynchronously. It was pointed out to me that my implementation, by firing handlers synchronously, violates Promise/A+, but other than the mere fact of that violation, what problems might I find with the approach I’m taking? |
@FGasper one reason to require async invocation of handlers is that it (perhaps counter-intuitively) makes your code more deterministic. somePromise.then(handlerA)
doThingB() If handlers are sometimes called sync and sometimes async, you cannot make any statements about ordering between This may seem like a subtle issue, but it prevents users from baking in accidental race conditions where they mistakenly think A will always happen before B, because it happened one time that they tested it. There may be other, better reasons for this requirement. This is just one I happen to think applies. |
Clause 2.2.4 of the promise/a+ spec says:
Then in the notes it states that:
Is the intention of this to ensure that when there is a large amount of onFulfilled functions in a chain, the execution of them does not cause the thread to block?
Or is there anything else that is between the lines that I am not reading?
The text was updated successfully, but these errors were encountered: