var {go, chan, putAsync, timeout} = require('js-csp'),
{map, mapSeries, filter, reduce, concurrent, series, waterfall} = require('./index.js');
function fetch(url){
var ch = chan();
require('superagent').get(url).end((err, res)=> {putAsync(ch, err || res); ch.close();});
return ch;
}
var urls = [
'http://www.google.com',
'http://www.jlongster.com',
'http://www.myntra.com'];
go(function*(){
yield map(urls, function*(url){
return (yield fetch(url)).text.length;
});
yield mapSeries(urls, function*(url){
return (yield fetch(url)).text.length;
});
yield filter(urls, function*(url){
return (yield fetch(url)).text.length > 100000;
});
yield reduce(urls, function*(weight, url){
return weight + (yield fetch(url)).text.length
}, 0);
yield concurrent([
function*(){ yield timeout(800); return 'a'; },
function*(){ yield timeout(200); return 'b'; },
function*(){ yield timeout(300); return 'c'; }]);
yield series([
function*(){ yield timeout(800); return 'a'; },
function*(){ yield timeout(200); return 'b'; },
function*(){ yield timeout(300); return 'c'; }]);
yield waterfall([
function*(x){ yield timeout(800); return x*2 },
function*(x){ yield timeout(200); return x+5 },
function*(x){ yield timeout(300); return 'c'; }], 2);
});