Created
March 13, 2012 20:28
-
-
Save s3u/2031348 to your computer and use it in GitHub Desktop.
IO fork/join more
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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