yew
v0.0.1
Published
Generator based flow-control
Downloads
6
Readme
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:
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:)
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
request('https://graph.facebook.com/GitHub', function(err, res, body) {
})
var data = yield [request, 'https://graph.facebook.com/GitHub'];
/*
data = [
0: err,
1: res,
2: body
]
*/
##Example
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