https://github.com/ddo/yew
Generator based flow-control
https://github.com/ddo/yew
Last synced: 5 months ago
JSON representation
Generator based flow-control
- Host: GitHub
- URL: https://github.com/ddo/yew
- Owner: ddo
- License: mit
- Created: 2014-03-25T09:44:52.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2014-03-29T16:49:04.000Z (about 12 years ago)
- Last Synced: 2023-12-22T17:02:04.195Z (over 2 years ago)
- Language: JavaScript
- Size: 148 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
yew
===
[](http://badge.fury.io/js/yew)
[](https://coveralls.io/r/ddo/yew?branch=master)
[](https://codeclimate.com/github/ddo/yew)

Generator based flow-control
##Caution
Since generators is a new feature in JavaScript ES6, so you need:
Node version: ~0.11.x
Then run with ``node --harmony-generators`` flag
##Installation
$ npm install yew --save
##Usage
###Async way:
```js
var request = require('request');
request({
url: 'https://graph.facebook.com/GitHub',
method: 'GET',
json: true
}, function(err, res, body) {
console.log(body);
});
```
###Yew way: (Sync way :smiley:)
```js
var yew = require('yew');
var request = require('request');
yew(function *() {
var data = yield [request, {
url: 'https://graph.facebook.com/GitHub',
method: 'GET',
json: true
}];
console.log(data[0]); //err
console.log(data[1]); //res
console.log(data[2]); //body
});
```
##How to use
* ``yield`` an array
* Array format: [function, argument1 [, argument2, ...]]
* No need callback function in array
* Callback of function must be the last argument
##Return
```js
request('https://graph.facebook.com/GitHub', function(err, res, body) {
})
```
```js
var data = yield [request, 'https://graph.facebook.com/GitHub'];
/*
data = [
0: err,
1: res,
2: body
]
*/
```
##Example
```js
var yew = require('yew');
var request = require('request');
yew(function *() {
var facebook = yield [request, 'https://graph.facebook.com/facebook'];
var tj = yield [request.get, 'https://graph.facebook.com/tjholowaychuk'];
var github = yield [request, {
url: 'https://graph.facebook.com/GitHub',
json: true
}];
console.log(facebook[2]);
console.log(tj[2]);
console.log(github[2]);
});
```
##Todo
* Error handler