Reputation: 4017
From the below code i'm not getting result1 data. It's undefined. Can somebody help me?
async.waterfall([
function(callback) {
request({
method: 'GET',
headers: {
'Content-Type': 'application/json'
},
url: url
}, function(error, response, body) {
if (error) {
callback(error);
} else {
var result = JSON.parse(body);
callback(null, result); //sending to next function
}
});
},
function(result, callback) {
async.eachSeries(result, function(item, callback) {
request({
method: 'GET',
headers: {
'Content-Type': 'application/json'
},
url: url+item.id
}, function(error, response, body) {
if (error) {
callback(error);
} else {
var result1 = JSON.parse(body);
callback(null, result1);
}
});
}, function(err, result1) {
if (!err) {
callback(null, result1);
} else {
callback(err);
}
});
},
function(result1, callback) {
console.log(result1); // getting undefined
request({
method: 'GET',
headers: {
'Content-Type': 'application/json'
},
url: url
}, function(error, response, body) {
if (error) {
callback(error);
} else {
var result2 = JSON.parse(body);
callback(null, result2);
}
});
}
], function(error, res) {
console.log(res); // process final result
});
Upvotes: 0
Views: 411
Reputation: 77482
For eachSeries
signature looks like this - callback(err), there is no second parameter, that's why result1
is undefined
If you need get results when loop has finished you need use .mapSeries
, like so
async.mapSeries(result, function(item, callback) {
request({
method: 'GET',
headers: {
'Content-Type': 'application/json'
},
url: url + item.id
}, function(error, response, body) {
if (error) {
callback(error);
} else {
var result1 = JSON.parse(body);
callback(null, result1);
}
});
}, function(err, result1) {
if (!err) {
callback(null, result1);
} else {
callback(err);
}
});
Upvotes: 2