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 😃)
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]);
console.log(data[1]);
console.log(data[2]);
});
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'];
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