co: add mz

This commit is contained in:
Rico Sta. Cruz 2015-09-21 13:32:35 +08:00
parent f868b37f59
commit 2ea54f4804
1 changed files with 17 additions and 1 deletions

18
co.md
View File

@ -6,6 +6,7 @@ title: co
[thunkify]: https://github.com/visionmedia/node-thunkify [thunkify]: https://github.com/visionmedia/node-thunkify
[unyield]: https://github.com/MatthewMueller/unyield [unyield]: https://github.com/MatthewMueller/unyield
[promisify]: https://www.npmjs.com/package/promisify [promisify]: https://www.npmjs.com/package/promisify
[mz]: https://www.npmjs.com/package/mz
[co] allows you to use generators to manage async flow. [co] allows you to use generators to manage async flow.
@ -44,7 +45,7 @@ get(function (err, res) { ... })
### Node callback → Thunk ### Node callback → Thunk
Use [thunkify]. You can yield this. You can also use [promisify] too. Use [thunkify]. You can yield this. You can also use [promisify].func too.
```js ```js
var readFile = thunkify(fs.readFile) var readFile = thunkify(fs.readFile)
@ -53,3 +54,18 @@ co(function * () {
var data = yield readFile('index.txt', 'utf-8') var data = yield readFile('index.txt', 'utf-8')
}) })
``` ```
### Using Node.js API
Uze [mz]. You can also either [thunkify] or [promisify].func Node.js API.
```js
var readFile = require('mz/fs').readFile
var getLines = co.wrap(function * (filename) {
var data = yield readFile(filename, 'utf-8')
return data.split('\n')
})
getLines('file.txt').then((lines) => { ... })
```