-
Notifications
You must be signed in to change notification settings - Fork 171
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
Callback ordering #77
Comments
Glad to see that this was raised for discussion. 😄 Just to throw my own argument into the pot... I argue that the fgh order is the correct one. Conceptually, Naturally, if |
The relationship between The order of I translate the code to the following English statements: |
Interesting! I was going to argue for var legendary = require("legendary");
var p = legendary.fulfilled();
function f(){ console.error("f"); };
function g(){ console.error("g"); };
function h(){ console.error("h"); };
p.then(f).then(g);
p.then(h); Which actually outputs This happens because the internal promise for |
I'm going to sit out of the argument for a bit, but FWIW...
I've implemented it in my own branch of when.js, and it's actually quite simple. Feel free to peruse if you want to compare and contrast the implementations. |
I am with @lsmith that this is not really up to the implementation. In particular, the fact that it's impossible to provide a consistent ordering in the general case, where promises are not immediately fulfilled, seems to nix any idea of specifying an order for the specific case of already-settled promises. |
Yea, as soon as I wrote my earlier commented I wanted to try out a trampoline. No optimizations or solid tests but seems to work.
If we don't specify an order, then surely it is up to the implementation? |
@novemberborn right, indeed, misspoke :). |
I know I said I'd sit out - I wanted to see how this progressed without reiterating arguments I've already made to @briancavalier - but I thought of something new. Sorry! The whole point of promises, from my perspective, is to allow you to write code that appears synchronous over future values. That suggests an obvious symmetry between synchronous and asynchronous code. Consider: p.then(f).then(g);
p.then(h); Would the synchronous analogue not be this? g(f(p));
h(p); That's phenomenally awesome, in my opinion. It's a very simple and straightforward translation, and it suggests that But how does evaluation order fit in? Unless something very fishy is going on, synchronous code follows the |
@Twisol how do you respond to the fact that if I think the synchronous analog of such a situation does not exist; rather, the asynchronous analog of g(f(p));
h(p); is p.then(function (pResult) {
f(pResult).then(function (fResult) {
g(fResult).then(function () {
h(pResult);
});
});
}); |
Two points:
|
The entire point of If you want order guarantees between |
As I said: anything else is freaking annoying in Javascript. I raise it for theoretical grounding, not suggested semantics. 😄 |
But it necessarily must be. Even with the status quo, this code will execute differently based on if p.then(f).then(g);
p.then(h).then(i); Returns a promise: |
Yes. That's my point. Order is not guaranteed, unless you explicitly do ordering with |
Implementations don't necessarily have to choose |
No, of course you're right about that. I just think that, in the same way that a random walk leaves something to be desired, an approach symmetric with synchronous code is easier to analyze. I'll return to the background for now. 😄 |
All I'm saying is you have your sense of symmetry wrong. See my example upthread. The only way to guarantee the same order of synchronous code is to actually explicitly lay out that order by putting your calls inside |
In particular, p.then(f).then(g);
p.then(h); has no synchronous analog, since in synchronous code, two operations (in this case |
I've love to discuss this with you one-on-one, because I'm not sure what you mean. I'm currently in the #cujojs channel on Freenode, but I can go elsewhere too. If you're cool with that, send me a private message or something. |
Maybe later; gotta head home and dinner and stuff now. Hopefully someone else can clarify, although I think my original example was clear as well. |
I think we're all in agreement here that Promises/A+ simply can't make a guarantee for any and all possible f's, g's, and h's (and developers will use any and all possible!). Let's just help developers embrace the g/h nondeterminism, and force them to guarantee order by using p.then(f).then(g).then(h) Closing, but as always, if there's a reason to reopen, feel free. |
I think its perfectly doable. The idea of writing async code which parallels sync is generally interesting too. |
I had this issue come up recently while discussing tick conflation algorithms with when.js contributor @Twisol. Given this:
In what order should f, g, and h be called? Promises/A+ currently says that f must be called before h, and that f must be called before g. It doesn't say anything about the relationship between g and h, so currently, afaict, both sequences, fgh and fhg, are correct.
I quickly tested current implementations of when.js, avow, and Q, and all 3 do breadth-first: fhg.
Anyone have any thoughts on this? I'm not necessarily saying we should (or even could) specify anything, but more just opening up the discussion to see where it leads.
The text was updated successfully, but these errors were encountered: