Created
September 6, 2013 11:48
-
-
Save idx3d/6462756 to your computer and use it in GitHub Desktop.
CO example
This file contains 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 request = require('superagent') | |
, co = require('co'); | |
var join = co.join; | |
var get = co.wrap(request.get); | |
var urls = [ | |
'http://google.com', | |
'http://yahoo.com', | |
'http://cloudup.com', | |
'http://ign.com' | |
]; | |
// sequential | |
co(function *(){ | |
for (var i = 0; i < urls.length; i++) { | |
var url = urls[i]; | |
var res = yield get(url); | |
console.log('%s -> %s', url, res.status); | |
} | |
}) | |
// parallel | |
co(function *(){ | |
var requests = urls.map(function(url){ | |
return get(url); | |
}); | |
var responses = yield join(requests); | |
console.log(responses.map(function(r){ | |
return r.status; | |
})); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment