Skip to content

Instantly share code, notes, and snippets.

@s3u
Created March 13, 2012 20:28
Show Gist options
  • Save s3u/2031348 to your computer and use it in GitHub Desktop.
Save s3u/2031348 to your computer and use it in GitHub Desktop.
IO fork/join more
var _ = require('underscore'),
http = require('http'),
URI = require('uri'),
async = require('async');
var sendReq = function (s, cb) {
// send a HTTP req, and pass error or result to the function arg
// content omitted for brevity
};
var sendMany = function (sources, cb) {
var funcs = [];
_.each(sources, function (source) {
funcs.push(function (s) {
return function (callback) {
sendReq(s, callback);
};
}(source));
});
async.parallel(funcs, cb);
}
var sources = ['http://www.google.com', 'http://twitter.com', 'http://www.ebay.com'];
sendMulti(sources, function(e, r) {
// process, decide and repeast
sendMulti(sources, function(e1, r1) {
// ...
sendReq(s1, function(e2, r2) {
// ...
});
// ...
sendMulti([..], function(e3, r3) {
// ...
});
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment