Continuations, yield, ES4

My language of choice for everyday coding is EcmaScript, thus I am very interested in everything new in this area. I'm mostly happy with the language, the only thing I miss in it seems to continuation support. I need them to simplify my work with asynchronous processing, that seems to happen very often in my development. I want them in order to convert this async processing to look like synchronous.

So for example when I need to perform a bunch of animations one after one I have to write just

for(var i=0;i<animations.length;i++)

What I need to write now instead is some hard to understand code.

Another asynchronous thing is communication with server (AJAX).

var someResult = serverProxy.callSomeFunc(arg);
var otherResult = serverProxy.callOtherFunc(someResult+1);

Now I am writing this in CPS stile, but it is a bit tedious.

  serverProxy.callOtherFunc(someResult+1, function(otherResult){
  //process something here

With continuations I could hide all the async-sync conversion in proxy and all the code will just use it in synchronous way.
So basically, I want SJAX instead of AJAX :-)

I know that JavaScript 1.7 in Firefox 2.0 supports yield keyword and generators.
But I do not know whether it is possible to achieve such kind of conversion using only yield. So that is my first question - is that possible at all.

Rhino already has full continuations support, and such things is possible to do in Rhino, but there is no Rhino in web-browser and in Flash player.

And second question is - does somebody know whether ES4 will support continuations or just yield. Somewher I read that Brendan Eich promised continuations, but I didn't find this in ES4 draft.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

No continuations

It was discussed a few months back on the es4 list if I recall and the response was a definite no. There are precompilers out there that give you what you want. Narrative Javascript for example. I believe Neil, the author of Narrative Javascript, also hopes to do a JS 1.7 compatible implementation that doesn't require precompilation (ie. is implemented only using yield).

Narrative Javascript

Narrative JS does the job, but it is precompiler, not a language feature. It will help in browser, but not Flash/ActionScript. And that will mean for me to have two separate code bases. It's also interesting what es4 proposes instead, or they just withdrawn continuations, cause it will be too hard to implement for language implementors.
I still hope that yield will be enough, but for now still do not have an idea whether it so or not.
I've found the discussion on continuations in es4 list. Reading it now.
Thanks for pointing on it.


You might want to have a look at haXe. It supports both browser (JS generator) and Flash (SWF 6-9) coding. There are no continuations in it yet, but it's planned for future releases. Depending on the target, it will be either supported by the underlying VM or simulated like Narrative JS does.

Generators and trampolining

Chris is correct -- I'm hoping to migrate Narrative JavaScript over to an implementation that supports generator syntax and uses trampolining to provide a concurrency implementation. The intent is that the pre-compiler would be used for implementations not compatible with JS 1.7. We'll see when/if I find time. :/

Writing a concurrency implementation using generators is fairly straightforward. There's a simple example in the Python PEP for generators. There's a couple key points missing from the example; perhaps I'll put together a working JS 1.7 implementation and post it on my blog. Let me know if you'd like to see that.

So, it's possible!

Started reading PEP 342. "Specification Summary" part says that the yield must be expression and 4 methods must be added. Checked the SpiderMonkey sources and found exactly those 4 methods. So it seems that Brendan followed this specification when implementing generators and JS 1.7 allows coroutines via trampolines.
At least I can use it on server-side and in XUL, that's already good news. And probably Adobe will add support in ActionScript in version 10, cause they are trying to be the first ES4 implementation avaible.
Other platforms will follow later.

And of course I am very interested in your implementation. I am regularly checking your blog, so hope to see this implementation in the nearest future. I will try to do it myself in free time though right now it looks not so straightforward for me.