## Using continuations for web programming

The Cocoon project has introduced FlowScript. Flowscript is a
modified
version of javascript which supports continuations and is used to write web applications that span pages.

Here is an example from the site of a calculator application which uses multiple pages to request the numbers and
operator.

"In this example, the calculator function is called to start the calculator application. We'd like the sendPageAndWait function to be a special function, that takes as arguments an HTML file to be sent as response, and some optional data that needs to be placed dynamically in it. We would like sendPageAndWait to send the response page and then block the executing thread, until the user clicks on a link in the response page, which sends a request back to the server. This request resumes the processing at the point it was left, right after the call to sendPageAndWait."


function calculator()
{
var a, b, operator;

cocoon.sendPageAndWait("getA.html");
a = cocoon.request.get("a");

cocoon.sendPageAndWait("getB.html");
b = cocoon.request.get("b");

cocoon.sendPageAndWait("getOperator.html");
operator = cocoon.request.get("op");

try {
if (operator == "plus")
cocoon.sendPage("result.html", {result: a + b});
else if (operator == "minus")
cocoon.sendPage("result.html", {result: a - b});
else if (operator == "multiply")
cocoon.sendPage("result.html", {result: a * b});
else if (operator == "divide")
cocoon.sendPage("result.html", {result: a / b});
else
cocoon.sendPage("invalidOperator.html", {operator: operator});
}
catch (exception) {
cocoon.sendPage("error.html", {message: "Operation failed: " + exception.toString()});
}
}



## Comment viewing options

### Continuations or wait loop?

I suppose it is not obvious that continuations are being used for this example.

It could just as easily be a wait loop.

Do you have an example that shows more obvious continuation-based behaviour?

### Re: Continuations or wait loop?

I added some text from the web site describing the example.

I suppose you could do this with a wait loop. However, since the web browser does not maintain a connection between page requests, a separate thread would be needed to to actually send and receive pages. That thread would then communicate with this thread.

### What happens if the user gets

What happens if the user gets to getB.html and then hits the back button? Does the script pick up after getA.html, or does it get confused and accept garbage data?

True continuations handle the back button gracefully. While being able to write webapps as functions is nifty, it isn't very helpful if it leads to eg. the user submitting the same order twice because they hit back.

Edit: The website apparently answers my question:

With this approach clicking the Back button in the browser is no longer a hassle to deal with for you as a server-side programmer. They will simply refer to past continuations objects, which have their own state of the local variables.

So it does apparently use true continuations. I wonder what implementation strategy (heap-is-stack, stack-copying, or cactus stacks) they used, and how efficient it is.

### Seaside Framework

I found another continuations based framework written in Smalltalk:
http://www.beta4.com/seaside2/

A paper describing the system can be found at: http://www.iam.unibe.ch/~scg/Archive/Papers/Duca04eSeaside.pdf (I haven't read it yet).

-Dave