On 20/10/2011, at 23:37, Brendan Eich wrote:
> On Oct 20, 2011, at 12:59 PM, Jorge wrote:
>
>> the assert_invariants() at the next line might run in another turn of the
>> event loop (when f() resumes), just as the callback does.
>
> No. Nothing in JS today, since it lacks coroutines or call/cc, can suspend
> under f and cause the continuation to be captured and then called in a later
> event loop turn.
That's why I put the comment //might suspend execution !
*IF* it had coroutines or call/cc, then
>> the assert_invariants() at the next line might run in another turn of the
>> event loop (when f() resumes), just as the callback does.
and then, as far as I can see, the risks wrt invariants would be exactly the
same in the two cases:
//#1
assert_invariants();
function callBack () {
assert_invariants(); // perhaps yes, perhaps no. There's no guarantee.
};
setTimeout(callBack, 1e3);
return;
//#2
assert_invariants();
f(); //might suspend execution
assert_invariants(); // perhaps yes, perhaps no. There's no guarantee either.
return;
And my point is that the invariants not invariant anymore argument against
call/cc (that the node.js guys keep harping on again and again) does not hold
for this kind of async code written in cps because this kind of async code
written in cps does not guarantee it either.
On the other hand, *IF* we could suspend f(), instead of:
asyncFunction(request, cb);
function cb (e, response) {
if (e) //whatever
//our code continues here
}
we could simply write the above like this:
try {
response = asyncFunction(request); //might suspend execution
}
catch (e) {
//whatever
}
//our code continues here
And this has several (valuable, imo) advantages:
- We aren't trashing the call stack on every async call: we can finally debug
properly!
- We can (finally!) catch the exceptions where and when it matters.
- We can loop and control flow in the usual ways (at last!).
- It's the habitual style of coding that everybody knows already.
--
Jorge.
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss