Skip to content

Instantly share code, notes, and snippets.

@threepointone
Last active February 25, 2017 16:15
Show Gist options
  • Save threepointone/92c2340476499287737e to your computer and use it in GitHub Desktop.
Save threepointone/92c2340476499287737e to your computer and use it in GitHub Desktop.
parallel / series requests with js-csp
var csp = require('js-csp'),
{ chan, putAsync, take, go, put, timeout, spawn} = csp,
request = require('superagent');
var urls = ['http://www.google.com', 'http://www.jlongster.com', 'http://www.myntra.com'];
go(function*(){
// 1. do a bunch of requests in parallel, and save their response lengths
var parallel = yield map(urls, function*(url){
return (yield fetch(url)).text.length;
});
console.log(parallel); // [ 19643, 12148, 285823 ]
// neat!
// 2. the same, but in series
var queue = yield mapSeries(urls, function*(url){
return (yield fetch(url)).text.length;
});
console.log(queue); // [ 19683, 12148, 285826 ]
});
// make a request, put its response on a channel
function fetch(url){
var c = chan();
request.get(url).end((err, res)=> {putAsync(c, err || res); c.close();});
return c;
}
function map(arr, gen){
return go(function*(){
var chs = arr.map(val => spawn(gen(val)));
var ret = [];
for(var i=0;i<chs.length;i++){
ret.push(yield chs[i])
}
return yield ret;
})
}
function mapSeries(arr, gen){
return go(function*(){
var ret = [];
for(var i=0;i<arr.length;i++){
ret.push(yield spawn(gen(arr[i])))
}
return yield ret;
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment