I've just started with Node.js and callbacks are something I'm getting used to it. Sometimes it just gives me a bad time. I have a function dwnloadData() which downloads the data and append the data into an empty array and send that array.
Here for downloading I'm using callback and I'm getting arrData empty Can someone pls let me know the correction.
Requirement: arrData, the array should have all the data downloaded using for loop and should return using promise.
downloadData(url) {
return new Promise((resolve, reject) => {
var arrData = [];
for (var i = 0; i < url.length; i++) {
request.get(url[i], function (error, response, body) {
if (!error && response.statusCode == 200) {
var content = body
var jsonArray = JSON.parse(content);
}
});
arrData.push(jsonArray)
}
resolve(arrData)
});
}