So what are challenges of using a continuation system? The biggest barrier for using continuations is that there is no native support in JavaScript for continuations (although JS 1.7 generators provide a form of coroutines that are a step in the right direction). Therefore it is necessary to do preprocessing or compiling of your JavaScript source code. The compilation process adds the necessary hooks for the continuation system to be able to suspend and resume call stacks. One of the great advantages of the Authenteo system is that the web editing tool automatically does source code compilation for you. Therefore your source can be easily managed by the content management system with support for continuations through out.
This continuation system is based on
Narrative JavaScript
which provides a JavaScript and Java based compiler if you want to start writing continuations without Authenteo.
Chris Double
also has an approach to using Narrative that involves compiling the code at runtime, however this has the disadvantage of increased overhead (compiler must be downloaded and compilation must take place).
One additional benefit of using Authenteo form of the Narrative JavaScript continuation system, is that it is not necessary to use extra syntax to indicate that a function call may suspend.
Another question that we might ask, is using continuations as efficient as hand writing your code? Well, it is true that you can achieve slightly improved performance by hand writing your code (just as you can often improve performance by writing assembly code), but for most situations that involve continuations, the cost of call stack suspend and resume are negligible compared to whatever the continuation is waiting on (remote call, setTimeout, or user interaction). However, it is certainly possible to write a function that handles the low level part of animation and uses the continuation to pause returning to the calling function until the animation is complete. That is one of the key flexibilities of this continuation system. You can write a function that does use callbacks internally, but make it pause for the sake of the calling function.
You may also have wondered what is actually in the sleep function. The sleep function simply uses setTimeout and uses the Future function to setup the pause (this is part of the Narrative JavaScript API).
function sleep(milliseconds) {
var notifier = new Future();
setTimeout(notifier,milliseconds);
notifier.result(); // pause until the setTimeout is finished
}
Finally, feel free to visit the
Narrative JavaScript
website to learn more about the API for continuations or email me at
kriszyp@xucia.com
. The Authenteo uses close to the same API (here is more about the Authenteo
API
), although the main difference is that the -> operator has the opposite meaning for Authenteo. Rather than indicating that the called function may yield/pause, it means that a new thread should be started for this function call (if the called function suspends).
Let me know if you want to
learn more
about what is possible with continuations, and I can write another article on the subject.