4 ms·
Node.js is built around a lot of continuations, if I recall. It's very common in event driven computing. I believe the basic way to do it is that the function
by Periodic 15y ago
Node.js is built around a lot of continuations, if I recall. It's very common in event driven computing.
I believe the basic way to do it is that the function that processes the original request both returns the output and the continuation, then the next time the user connects it would use the continuation instead of the original function.
function doHTTPRequest(requestVars, renderPage)
{
(content, renderNextPage) = renderPage(requestVars);
return (content, function (newRequestVars) {
renderNextPage(newRequestVars);
});
}
It's a little awkward, and maybe not the best example of CPS. But basically what happens is the request gets handled and returns the output and something to do next. Then you return your output and some additional computation that will get executed later, in this case a function that renders a different page. This is computation that hasn't been evaluated, but which can be passed around to evaluate later. Note that this whole function could be replaced by renderPage, but I wanted to make things more explicit. A good language would let you write
doHTTPRequest = renderPage;
Also, when your config would probably specify that it should call renderPage and so that function would get passed around or held by your controller/dispatcher until the actual doHTTPRequest is called.
- jiggy2011 15y agoThankyou, that has made it at least slightly more clear to me I think :) So what you need basically in the core of your server is some data structure (possibly keyed by the client's IP address and TCP port number?) that contains a bunch of functions with prepopulated arguments (curried functions?). The prepopulated arguments are basically the state set by the previous request. When a new request comes in you somehow match it up with your data structure and execute? I guess the continuation part is to force the I/O to happen at the right point, i.e make the next request depend on the execution of the first? Of course Javascript is not really functional so that needn't be explicit.
- lemming 15y agoWhat you're describing is CPS, but that's not how continuations would typically be used in a webapp, it's actually a compiler technique (I've always thought it was crazy that node.js forces you to do by hand something that should be done by the compiler). A language that supports full continuations as first class objects, for example some Scheme implementations, allow you to actually suspend the current execution (including the entire call stack) and store it somewhere, and then start executing it again at a later point in time. So your web container can start running a thread of execution for a particular request, suspend it after it writes its response then resume it again when it receives the next request corresponding to this web session. It allows you to write code like: function handleSession() { renderFormPage() // writes response with HTML for form suspend() // wait for next request in the session processForm() // process the form data - notice this is the same function } It's a very powerful technique and can make web programming very intuitive since you can have a single linear flow for a complex web interaction. Check out Chris Double's tutorial on this: http://double.co.nz/scheme/modal-web-server.html http://double.co.nz/scheme/modal-web-server.html Or SISC-Web for a simple but complete implementation: http://siscweb.sourceforge.net/overview.html http://siscweb.sourceforge.net/overview.html