https://github.com/singcl/co
🎷 A counterfeit co & comprehension of thunk function and function* in javascript.
https://github.com/singcl/co
co generator promise thunk
Last synced: about 2 months ago
JSON representation
🎷 A counterfeit co & comprehension of thunk function and function* in javascript.
- Host: GitHub
- URL: https://github.com/singcl/co
- Owner: singcl
- Created: 2018-05-01T14:48:23.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-05-07T15:33:06.000Z (over 7 years ago)
- Last Synced: 2025-09-25T14:17:10.700Z (3 months ago)
- Topics: co, generator, promise, thunk
- Language: JavaScript
- Homepage:
- Size: 92.8 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## CO
[](https://www.npmjs.com/package/@singcl/co)


[](https://www.npmjs.com/package/@singcl/thunk-run)
*A counterfeit co refer to [co](https://github.com/tj/co/tree/master).*
Generator based flow-control goodness for nodejs, using thunks or promises, letting you write non-blocking code in a nice-ish way.
### Installation
`npm install @singcl/co -S`
### Example
```js
var fs = require('fs');
var path = require('path');
var co = require('@singcl/co');
var thunkify = co.thunkify;
var promisify = co.promisify;
var reaFileThunkify = thunkify(fs.readFile);
var reaFilePromisify = promisify(fs.readFile);
var filePath1 = path.resolve(__dirname, './test.txt');
var filePath2 = path.resolve(__dirname, './co.exam.js');
co(function* (path) {
var a = yield reaFileThunkify(path, 'utf8');
var b = yield reaFileThunkify(path, 'utf8');
var c = yield reaFileThunkify(path, 'utf8');
console.log(a);
console.log(b);
console.log(c);
}, filePath1).then(function(v) {
console.log('co完成!', v);
});
co(function* (path) {
var a = reaFilePromisify(path, 'utf8');
var b = reaFilePromisify(path, 'utf8');
var c = reaFilePromisify(path, 'utf8');
var res = yield [a, b, c];
console.log(res);
}, filePath2).then(function(v) {
console.log('co完成!', v);
});
```
### Yieldables
The "yieldable" objects currently supported are:
- promises
- thunks (functions)
- array (parallel execution)
- generators (delegation)
- generator functions (delegation)
### API
**API about more is at TJ`s [co](https://github.com/tj/co/tree/0.5.0)**