Wednesday, August 19, 2009

JavaScript Continuation Support? 2: Strands

Strands did what I expected (though still need to write my actual app using it :)):


function sleep(millis) {
var future = new Future();
setTimeout(future.fulfill, millis);
future.result();
}


function go () {
document.getElementById("txtr").innerHTML += "1" ;
sleep(2000);
document.getElementById("txtr").innerHTML += "2" ;

}

function go2 () {
go();
document.getElementById("txtr").innerHTML += "3" ;
}


new Future(go2);


Yields "123". Requiring "new Future(go2)" isn't that wonky to work with in terms of my transparency goals: just wrap the entire program in a thunk and call it as a future.

Setting up the library took a bit of editing: line 344 of strand.js had a strange eval statement that wasn't parsing so I removed it (Firefox 3.5.2). Depending on how you invoke it, you might need to change some hardcoded default paths. I couldn't get the compiler mode working, unfortunately. We'll see if it can handle big APIs :)

No comments: