I have this array with rest-api paths:
var paths = ['path1','path2','path3'];
I want to create an array with results from each path. In this case I instead use 'http://www.google.com/index.html' instead of 'path'
exports.test = function(req, res){
var paths = ['path1','path2','path3'];
var resultArr = [];
async.each(paths, function(path, cb){
console.log('collecting data from: ' + path);
http.get('http://www.google.com/index.html', function(result){
resultArr.push(result);
console.log('Done collecting data from: ' + path);
cb();
});
}, function(){
console.log('Done collecting data from all paths');
res.status(200).send('hello');
});
};
This logs:
Starting server at 127.0.0.1:5000
collecting data from: path1
collecting data from: path2
collecting data from: path3
Done collecting data from: path2
Done collecting data from: path1
Done collecting data from: path3
Done collecting data from all paths
GET /test 304 128.752 ms - -
It's not waiting for a a call to complete. I want to get the results in series, one by one. What am I doing wrong?