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
nearly Promises/A+ compliant via Promiz.mithril.js #169
Conversation
Just curious, why is it only "nearly" compliant? |
Unfortunately it is impossible to make it compliant because this implementation does not execute asynchronously:
For my purposes I don't mind it being this way (which is why I left it as is), though others may have stronger feelings about it. |
See promises-aplus/promises-spec#4 and the issues referenced there for a thourough discussion. It is IIRC a matter of predictability and security. If the resolution is allowed to be synchronous, it is possible for an attacker to make the promise resolve synchronously or asynchronously, potentially bypassing some initialization code in the first case. I see that your |
@pygy, because the tests were written synchronously (backwards compatibility), and because of the 4-10ms delay caused by setTimeout, as commented here: #1 (comment) |
@pygy Am I reading that wrong, or does the thread basically end with the conclusion that async is bad? |
Some people are complaining, but the authors of the spec are very clear that async is the way to go. The crux of the argument is apparently in the second comment of this thread: promises-aplus/promises-spec#139. You must read the first one for context. To be honest, I don't understand what erights means by invariants, but it convinced Domenico, so I suppose it makes sense :-). |
@pygy this article ( http://blog.izs.me/post/59142742143/designing-apis-for-asynchrony ) clearly explains how an interface that is "somethings sync vs sometimes async" makes debugging hard. It allows the introduction of "synchronous race conditions" |
@Raynos: I understand the general point about race conditions, what I don't get is what erights means by "invariant". |
@pygy by invarient he means this. function MyProto() {
this.state = "cat"
this.somePromise = new Promise();
}
MyProto.prototype.foo = function () {
this.state = "dog";
this.somePromise.resolve();
assert.equal(this.state, "dog"); // synchronous resolution can mutate this.state
} The invariant that an object's state is not mutated inside a method is broken if a promise sometimes resolves synchronously and sometimes asynchronously. @pygy the design by contract article on wikipedia ( http://en.wikipedia.org/wiki/Design_by_contract ) has good reference on what it means to have "invariants upheld" |
@Raynos Thanks for the clarifications :-) |
Conflicts: mithril.js
@lhorie - Can we get this merged? |
Just to note- this fix also resolves an issue whereby promises swallow any errors in a promise success callback. I experienced this specifically with undefined variable errors in |
Added a (nearly) Promises/A+ compliant implementation based off of Promiz.micro.js (the smallest Promises/A+ implementation, at 228bytes min + gzip)
I apologize for the whitespace edits, that was my editor.
Also note that I had to change two tests
This mostly addresses #1