| |
Continuations and callbacks
http://stackoverflow.com/questions/14019341/whats-the-difference-between-a-continuation-and-a-callback
Continuations and callbacks
http://stackoverflow.com/questions/14019341/whats-the-difference-between-a-continuation-and-a-callback
------------------------------- A callback function ------------------------------- What is a Callback? When a call is made to an entity that is external to your JavaScript code, like for example a server, your code cannot usually afford to wait for a reply because that reply may be several seconds in coming. Furthermore, there are likely to be other calls that need to be made without delay and you just cannot hang around waiting for something that may or may not happen.
Suppose you call a function that explicitly returns a value, and that return just never happens? You code will simply hang, waiting for forever for a return value that never arrives.
The solution to this problem is to use callbacks — that is to pass along with the call to the server a JavaScript function that the server should invoke whenever the result is ready.
A callback function is a function that is passed to something else, which will later call the function to notify the user of something. An event handler method is an example of a callback function. Callbacks are a specific case of continuations. To quote PFPL, ch 30.
[first class] continuations... are ordinary values with an indefinite lifetime that can be passed and returned at will in a computation. Continuations never “expire”, and it is always sensible to reinstate a continuation without compromising safety. Thus continuations support unlimited “time travel” — we can go back to a previous point in the computation and then return to some point in its future, at will.
Why are continuations useful? Fundamentally, they are representations of the control state of a computation at a given point in time. Using continuations we can “checkpoint” the control state of a program, save it in a data structure, and return to it later ------------------------------- Continuations ------------------------------- In a language with first class functions we may pass the control and return value to a callback instead of explicitly returning to the caller In almost all languages functions explicitly return values (and control) to their caller, in our case "function add is caller". For example:
var sum = add(2, 3);
alert(sum);
function add(x, y) { return x + y; }
----------------------------------------- returning a value to a continuation ----------------------------------------- add(2, 3, function (sum) { alert(sum); });
function add(x, y, cont) { cont(x + y); } -----------------------------------------
Thus instead of returning a value from a caller function we are continuing with another function. Therefore this function is called a continuation of the first.
A continuation is actually a reification of the control state of the program: a snapshot of the state of the program at a certain point in time. The fact that it can be called like a normal function is irrelevant. Continuations are not actually functions. Callbacks on the other hand are actually functions. That's the real difference between continuations and callbacks. Nevertheless JS doesn't support first-class continuations. Only first-class functions. Hence continuations written in CPS in JS are simply functions.
A continuation need not be a function (or procedure as I called it). By definition it is simply the abstract representation of things yet to come. However, even in Scheme a continuation is invoked like a procedure and passed around as one.
Although some believe that continuations are a special case of callbacks. A function may callback any number of functions, any number of times. It's also a bit odd to call continuations a special case of callbacks. I can see how they are easily grouped together, but continuations didn't arise from the need to distinguish from a callback.
However if a function calls back another function as the last thing it does then the second function is called a continuation of the first.
A continuation actually represents the instructions remaining to complete a computation, or the remainder of the computation from this point in time.
You can think of a continuation as a computational hole that needs to be filled in. If I can capture a program's current continuation, then I can go back to exactly how the program was when I captured the continuation. (That sure makes debuggers easier to write.)
In this context, the answer to your question is that a callback is a generic thing that gets called at any point in time specified by some contract provided by the caller [of the callback]. A callback can have as many arguments as it wants and be structured in any way it wants. A continuation, then, is necessarily a one argument procedure that resolves the value passed into it. A continuation must be applied to a single value and the application must happen at the end. When a continuation finishes executing the expression is complete, and, depending on the semantics of the language, side effects may or may not have been generated.
If a function calls another function as the last thing it does then it's called a tail call. Some languages like Scheme perform tail call optimizations.
|
|