John Lam 1997 — 2026

JavaScript and Continuation Passing Style

I’ve been writing a lot of JavaScript code for work recently, and almost all of it involves async operations. Most of the code that I write is using continuation passing style. It’s quite a natural style of programming in JavaScript since functions are first class objects in JavaScript. It’s also interesting that closures and higher order functions work so beautifully together.

Consider this simple wrapper around XMLHttpRequest:

``` function get_url(file, completion) { var x = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"); x.onreadystatechange = function () { if (x.readyState == 4 && x.status == 200) completion(x.responseText); }; x.open('GET', file, true); if (window.XMLHttpRequest) x.send(null); else x.send(); } ```

Most of the cruftiness in this function involves accounting for differences between IE and Firefox, but you can see that the continuation is passed in via the completion parameter, and I’m using a closure to capture the x variable in an anonymous function that’s called when the async HTTP GET request has completed processing. The anonymous function then calls the continuation, passing the result of GET.

Just last night I was reading this excellent paper on functional programming. It explains what I am doing so much more elegantly than I can (although I hope the code speaks for itself). I highly recommend that you spend some time reading it so that you can get what all of this functional programming hoopla is all about. I assure you, it’s the real deal.

4 archived reader comments

Jason Bock (2006-06-30)

I used to think JavaScript was a kiddie language. Then I used it for the last year and I saw just how powerful it really is. I still find it a bit clunky in spots but overall my view of JavaScript is much, much brighter.

John Lam (2006-06-30)

Likewise. When I saw the Y-Combinator expressed so succinctly in JavaScript my mind was forever changed …

Sam Smoot (2006-07-07)

Is it just me, or does CPS seem like an odd name for it? Maybe it’s just because I’ve had so much trouble with wrapping my head around continuations in Ruby, but I would tend to simply call this passing a closure (assuming your anonymous function captured anything, otherwise it’s just function passing I suppose).

Besides the intent, this doesn’t really do anything an iterator doesn’t already do.

I mean, I suppose the intent here is pretty important. Calling it a “continuation” just struck me as odd… not that you’re wrong of course. I mean, it’s right there on wikipedia. It’s just that, without stack-swapping, is it really a continuation? I (perhaps naively) thought that was the real key to understanding them when I read “Continuations in Ruby”:http://wiki.rubygarden.org/Ruby/page/show/ContinuationExplanation.

One thing I found recently though: This is an abysmally slow style of programming in JavaScript. My conclusion was because JavaScript is pass-by-value, but what do I know? It’s sad too, because I was all excited about just how much I was able to bend JavaScript to my will until I realized writing anything like ActiveRecord in JavaScript just isn’t practical from a performance standpoint.

Anyways, sorry for rambling. And if you get a chance to look at my RubyClr bug-repo on RubyForge I’d appreciate it. (“subtle” is my middle name!) :-)

John Lam (2006-07-07)

Sam, I highly recommend reading the defmacro article that I linked to in the post. It really does an excellent job of linking CPS to call_cc style programming which is really just the same thing, but with “branching” in the stack.

Wow - it’s cool that you did go down that path (to create an ActiveRecord in JS). Which JS implementation were you using BTW - was it Rhino?

I have your repro on my list of things to do in the next couple of days. I just finished up a small project for a customer, and I’m taking a break by writing some more fun WPF code using Ruby. There’s a lot of things that I’d like to fix in RubyCLR, and I’m hoping to add to that list by breaking it with some WPF code :)