visionmedia/jade — pull request #1396 by jeromew (2014-01-26 01:37:26+00:00)
Title: jade prototype: async rendering with co and yielding locals

Hello,

I am currently looking at koa to see what new things we can do with generators and ran into the idea that maybe the jade rendering phase could be done asynchronously.

I haven't tested everything but surprisingly with a very small footprint patch, jade rendering can become async :-)

This could be useful in some scenarios when you want to start streaming content to a user even though all the data has not yet been fetched.

I hope you will find it useful & would be thrilled if it could make it upstream in this form of another :-)

I wrote the PR so that it does not break the existing jade. It should keep working just as before except for the new harmony: true compiler option.

to invoke the compiler with generators enabled, you have to add in the options :

harmony: true
self: true // this is necessary otherwise uglify complains about parsing the jade templates containing yield

I added a test in mocha tests, run with

mocha --harmony -R spec test/run.js

it will run the tests in test/yield-cases

The test is a bit contrived but I just wanted to show an example at this stage.

```
- var title = yield locals.title
h1= title
ul
- var dirs = yield locals.readdir('test/yield-cases')
for file in dirs
- var stats = yield locals.stat('test/yield-cases/' + file)
li= file + (stats.isFile() ? " is a file": " is not a file")
```

I have not yet tested with extend, include or blocks + have not modified the existing tests to check that they work in async mode.