3 category: JavaScript libraries
8 [co](https://github.com/tj/co) allows you to use generators to manage async flow.
11 [co]: https://github.com/tj/co
12 [thunkify]: https://github.com/visionmedia/node-thunkify
13 [unyield]: https://github.com/MatthewMueller/unyield
14 [thenify]: https://www.npmjs.com/package/thenify
15 [mz]: https://www.npmjs.com/package/mz
17 ### Running generators
21 yield Promise.resolve(true)
25 A generator can `yield` a thunk or promise. Using `co()` will immediately invoke the block inside it.
27 ### Generator → Promise
30 var fn = co.wrap(function * (val) {
31 return yield Promise.resolve(val)
37 Use `co.wrap()`. Most of the time, you'll be using co.wrap.
39 ### Generator → Node callback
42 var get = unyield(function * () {
45 get(function (err, res) { ... })
48 Use [unyield]. (You can [thunkify] this later)
51 ### Node callback → Thunk
54 var readFile = thunkify(fs.readFile)
57 var data = yield readFile('index.txt', 'utf-8')
61 Use [thunkify]. You can yield this. You can also use [thenify] too.
66 var readFile = require('mz/fs').readFile
68 var getLines = co.wrap(function * (filename) {
69 var data = yield readFile(filename, 'utf-8')
70 return data.split('\n')
73 getLines('file.txt').then((lines) => { ... })
76 Use [mz] for async Node.js API. You can also either [thunkify] or [thenify] them instead.