javascript & call/cc

Consider simple example:
function foo() performs a simple AJAX call bar() (web method in ASPX terminology), that returns a value.
Something like:

function foo()
{
   return pageMethods.bar();
}

The code above is wrong, of course. Ajax call is asynchronous; it does not return value immediately. Instead, it takes a callback function as a parameter:

function foo()
{
   calback=function(result)
   {
     alert(result);
     return result;
   }
   return pageMethods.bar(callback);
}

But we still have a problem, because foo returns immediately, and alert is called only later. foo returns nothing.
Call-with-current-continuation could solve this problem, but javascript doesn’t have it.
Of course, there is (more than one) way to solve this, but I don’t know any nice way to do it.

I’ve another blog that talks about continuations and javascript: http://marijn.haverbeke.nl/cps/

Advertisements

Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: