In the Mozilla Promise documentation, there is a Guarantees section:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises#Guarantees
It contains the following statement:
Unlike "old-style", passed-in callbacks, a promise comes with some guarantees:
Callbacks will never be called before the completion of the current run of the JavaScript event loop.
This makes sense to me and is the behavior I would expect, but I'm looking for some additional documentation supporting this "Guarantee".
Is there some sort of spec that explicitly documents / requires this? Is this guaranteed for any javascript runtime that provides a native Promise implementation? Is it the wild west, and you can't depend on it, etc.
Is there some sort of spec that explicitly documents / requires this?
The spec used by promise libraries is the promise A+ spec. This particular requirement is number 2.2.4. Pretty much any promise library you use will obey this, though if you need to check this, there are tests which can verify compliance with the spec
More importantly, now that promises are a part of the language, native promises are governed by the ECMAScript specifications. Promises were first introduced in the 2015 edition, in section 25.4. The latest completed edition as of the time of this answer is the 2018 edition. The specific behavior you asked about is due to the section governing the .then method (found here), and the section on triggerPromiseReaction (found here). Each of those in turn references the enqueueJob operation (found here)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With