co: update

This commit is contained in:
Rico Sta. Cruz 2017-10-27 12:49:03 +08:00
parent 36b907944e
commit d557761007
No known key found for this signature in database
GPG Key ID: CAAD38AE2962619A
1 changed files with 16 additions and 12 deletions

28
co.md
View File

@ -1,6 +1,11 @@
--- ---
title: co title: co
category: JavaScript libraries category: JavaScript libraries
layout: 2017/sheet
updated: 2017-10-27
weight: -1
intro: |
[co](https://github.com/tj/co) allows you to use generators to manage async flow.
--- ---
[co]: https://github.com/tj/co [co]: https://github.com/tj/co
@ -9,21 +14,17 @@ category: JavaScript libraries
[thenify]: https://www.npmjs.com/package/thenify [thenify]: https://www.npmjs.com/package/thenify
[mz]: https://www.npmjs.com/package/mz [mz]: https://www.npmjs.com/package/mz
[co] allows you to use generators to manage async flow.
### Running generators ### Running generators
A generator can `yield` a thunk or promise. Using `co()` will immediately invoke the block inside it.
```js ```js
co(function * () { co(function * () {
yield Promise.resolve(true) yield Promise.resolve(true)
}).then(...) }).then(...)
``` ```
### Generator → Promise A generator can `yield` a thunk or promise. Using `co()` will immediately invoke the block inside it.
Use `co.wrap()`. Most of the time, you'll be using co.wrap. ### Generator → Promise
```js ```js
var fn = co.wrap(function * (val) { var fn = co.wrap(function * (val) {
@ -33,9 +34,9 @@ var fn = co.wrap(function * (val) {
fn().then(...) fn().then(...)
``` ```
### Generator → Node callback Use `co.wrap()`. Most of the time, you'll be using co.wrap.
Use [unyield]. (You can [thunkify] this later) ### Generator → Node callback
```js ```js
var get = unyield(function * () { var get = unyield(function * () {
@ -44,9 +45,10 @@ var get = unyield(function * () {
get(function (err, res) { ... }) get(function (err, res) { ... })
``` ```
### Node callback → Thunk Use [unyield]. (You can [thunkify] this later)
Use [thunkify]. You can yield this. You can also use [thenify] too.
### Node callback → Thunk
```js ```js
var readFile = thunkify(fs.readFile) var readFile = thunkify(fs.readFile)
@ -56,9 +58,9 @@ co(function * () {
}) })
``` ```
### Using Node.js API Use [thunkify]. You can yield this. You can also use [thenify] too.
Uze [mz] for async Node.js API. You can also either [thunkify] or [thenify] them instead. ### Using Node.js API
```js ```js
var readFile = require('mz/fs').readFile var readFile = require('mz/fs').readFile
@ -70,3 +72,5 @@ var getLines = co.wrap(function * (filename) {
getLines('file.txt').then((lines) => { ... }) getLines('file.txt').then((lines) => { ... })
``` ```
Use [mz] for async Node.js API. You can also either [thunkify] or [thenify] them instead.