Asynchronous, streaming embedded Javascript templating engine.

#Asynchronous, streaming embedded Javascript templates

AsEjs does pretty much the same thing that EJS templates templates do, with the following delicate differences:

  • Streaming: Rather than rendering the template completely into a buffer first, and then outputting it to the client, AsEjs templates output chunks of their content directly and immediately into the output stream.

  • Asynchronous: AsEjs support calling asynchronous methods from within the template, i.e. methods that return their result at a later time using a callback mechanism. The template simply pauses the output stream until the callback method gets called, then continues rendering.

Both features, combined with Asynchronous Futures, allow a web server to output the static headers of their response immediately when a request comes in, while waiting until data from the database is loaded. The remaining parts of the response are rendered in chunks as soon as the required data becomes available. This gives the browser a chance to already load assets of the web page while the server is still loading data. This effectively paralellizes the generation of the response and its rendering on the client.

It also helps prevent callback soup on Node.js servers, since the asynchronous ways of loading data are encapsulated in futures.

On the server, in the controller.

var file = new AsyncFuture fs.readFile, 'filename'
var user = new AsyncFuture, 'SELECT * from users where id=123'
<!-- This part is output immediately. -->
    <!-- Load CSS tags here. -->
    <style tags...>
    <!-- Load JS scripts here. -->
    <javascript tags>
    <!-- Render the static header here. -->