Saturday, September 02, 2006

Introduction to Continuations

Introduction to Continuations

The above link points to a really handy article on continuations that gives you some meaningful examples.

Why should you care about continuations?

Consider your typical AJAX call. Something like:

  var result = ajaxCall(uri, {query: "What is a Continuation"});

In the above call, the value of result isn't really useful. That's because ajaxCall kicks off its work in the background and returns immediately. The typical way to deal with this is to change the above call to:

  ajaxCall(uri, {query: "What is a Continuation"}, handlerFunction);

When the AJAX call is done, it will invoke handlerFunction.

This concept of providing the next function to call, rather than depending on a return value is more or less using continuations.

By taking in the function to call next, instead of returning back a value, you can do all sorts of nifty things. Like implement threads, or write a system that backtracks (like a regular expression engine).

But don't take my word for it, read the above link and try out the examples.

2 comments:

  1. Anonymous12:36 AM

    coincidentally, i just got done skimming over the code for logowiki, which you'd probably get a kick out of if you've yet to see it. its logo interpreter is written entirely in javascript, and cleverly (at least to me) uses CPS to control execution speed and stack depth. i just finished a PoPL course last spring and did well enough, but writing and deciphering functions written in CPS still makes my head spin a bit.

    oh, and i want to take this time to put money on continuation-based web frameworks like seaside (and SISCweb, of course) being the "next big thing." :P

    some good stuff in this blog, mr. simon. cheers.

    ReplyDelete
  2. Thanks for the pointer to logo. I'm a huge fan of logo as a language and a concept.

    As you can see from my past posts on SISCweb I'm sold on the concept too :-).

    Good luck, you are so on the right track.

    ReplyDelete