I would like to launch asynchronous http calls on my server node, i saw the async
node module and i guess the async.parallel
enables us to do that.
The documented example is pretty clear, but i don't know how i could manage multiple http calls.
I tried the example bellow but it doesn't even launch the http calls:
var http = require('http');
var Calls = [];
Calls.push(function(callback) {
// First call
http.get('http://127.0.0.1:3002/first' callback);
});
Calls.push(function(callback) {
// Second call
http.get('http://127.0.0.1:3002/second' callback);
});
var async = require('async');
async.parallel(Calls, function(err, results) {
console.log('async callback: '+JSON.stringify(results));
res.render('view', results);
});
If i launch the http requests separately, i do have a result, but but calling the async callback i get async callback: [null,null]
dont use capital names for other purpouses than types/classes
below is your code with corrected obvious mistakes
Ok the thing is to call the callback this way
callback(null, res);
insteadcallback(res);
, i think the first parameter is interpreted as an error and the second one is the real result.Have a look at the documentation:
You are creating a request, but you are not finalizing it. In your calls you should do:
This is assuming you are doing a normal GET request without body.
You should also consider using http.get which is a nice shortcut:
Update The other thing is that callbacks in async have to be of the form
The way you are doing it now won't work, because callback to http.get accepts only one argument
res
. What you need to do is the following: