This commit is contained in:
TJ Holowaychuk 2013-09-02 19:31:11 -07:00
parent 1680639232
commit 2733c61d00
2 changed files with 23 additions and 1 deletions

View file

@ -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('');
}
});
```

View file

@ -15,7 +15,7 @@ app.use(function(){
}); });
this.type = 'markdown'; this.type = 'markdown';
this.body = files.join(', '); this.body = files.join('');
} }
}); });