Wednesday, August 19, 2009

JavaScript Continuation Support? 1: NarrativeJS

Narrative JS enables you to define a sleep function in JavaScript:


function sleep (ms) {
var n = new EventReceiver();
setTimeout(n, ms);
n.wait->(); //block until n called
}


Now, we can sleep between printing without manual CPSing:


function writer() {
document.write("1");
sleep->(1000);
document.write("2");
}
writer();


This is close to what I wanted.. but not enough. Consider the following:


writer();
document.write("3");


I expected to see "1", and then, a second later, "123". Unfortunately, I saw "132"! The magic incantation would have been:


writer->();
document.write("3");


Essentially, the extension broke function encapsulation with respect to control. I can see merit to it -- rewriting becomes a local process. I want to truly and transparently write my asynchronous code in a synchronous manner (for a reason to be disclosed later): NarrativeJS helps clean up callback code while maintaining basic callback-style code structure (so you don't worry differently about a UI event handling mucking with state between callbacks), but it doesn't restore the synchronous semantics.

This is great... but I want the transformation to be global: sleep(), not sleep->(). Hopefully Strands fares better.
Post a Comment