From 2733c61d0008d50a877b6bc6ce7cf181d5ae4402 Mon Sep 17 00:00:00 2001 From: TJ Holowaychuk Date: Mon, 2 Sep 2013 19:31:11 -0700 Subject: [PATCH] docs --- docs/guide.md | 22 ++++++++++++++++++++++ examples/co.js | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/docs/guide.md b/docs/guide.md index 38a0210..ec07fe6 100644 --- a/docs/guide.md +++ b/docs/guide.md @@ -133,5 +133,27 @@ function logger(format){ } ``` +## Async operations + + The [Co](https://github.com/visionmedia/co) forms Koa's foundation for generator delegation, allowing + you to write non-blocking sequential code. For example this middleware reads the filenames from `./docs`, + and then reads the contents of each markdown file in parallel before assigning the body to the joint result. +```js +var fs = require('co-fs'); + +app.use(function(){ + return function *(){ + var paths = yield fs.readdir('docs'); + + var files = yield paths.map(function(path){ + return fs.readFile('docs/' + path, 'utf8'); + }); + + this.type = 'markdown'; + this.body = files.join(''); + } +}); +``` + diff --git a/examples/co.js b/examples/co.js index 3ba50f4..f8a995c 100644 --- a/examples/co.js +++ b/examples/co.js @@ -15,7 +15,7 @@ app.use(function(){ }); this.type = 'markdown'; - this.body = files.join(', '); + this.body = files.join(''); } });