Narrative JavaScript is a small extension to the JavaScript language that enables blocking capabilities for asynchronous event callbacks. This makes asynchronous code refreshingly readable and comprehensible.
Why?
In JavaScript your code can't simply wait until an event has fired -- the event must always be handled by a separate, asynchronous event handler. Sometimes this is fine, but it often forces what ought to be a simple sequence of statements into gnarly contortions. Narrative JavaScript provides a yielding operator for functions which block until the firing of an event. The function yields execution until the callback is fired, and then resumes execution at the point where it yielded. This allows you to write hard-to-read asynchronous event handling in simple, linear, readable code.
This library is the result of years of personal experience coding and debugging mind-warping, impossibly complicated callback-based "AJAX" applications. It is the author's hope that this library may reduce this pain for others.
How it Works
Narrative JavaScript consists of a compiler and a runtime library. You write your code code as linear Narrative JavaScript (using the yielding operator) and the compiler will parse and translate the code into normal (asynchronous) JavaScript. You then deploy the compiled code along with a runtime library.
Since generated code is often difficult to read, great care is taken to make Narrative JavaScript as debugging friendly as possible: line numbering of the source code is kept intact in the compiled code, stack ordering is preserved, and exception support does not result in unnecessary catching and re-throwing of exceptions.
The Narrative JavaScript parser is based off of Brendan Eich's narcissus, a JavaScript parser written in JavaScript for the Mozilla project.
Project Status
Narrative JavaScript is currently in Beta. New as of the Beta release:
- Client side load-and-compile API based on XHR
- Formal threading API
- Java-based compiler using Rhino
- bug fixes
Download
Documentation and Reference
Mailing List
You can sign up for a Narrative JavaScript mailing list at http://groups.google.com/group/narrativejs, or you can send an email to narrativejs -at- googlegroups -dot- com. The mailing list is an open forum for support, feature requests, bug reports, or general Narrative JavaScript discussion. All are welcome to join.
License
Narrative JavaScript is released under a combination MPL/GPL/LGPL tri-license.