I have a function:
var third = function(classes){
for (var i = 0; i <= (classes.length-1); i++) {
var Myurl = classes[i];
return function(Myurl){
request(Myurl, function(err, resp, body) {
if (!err && resp.statusCode == 200) {
var $ = cheerio.load(body);
$("#item_details dl").each(function() {
var Values = [];
var New = [];
Values=$(this).find("dd strong").text();
New = Values.replace(/[\n\t\r]/g,"");
AllLinks.push(New);
});
console.log(AllLinks);
};
})
}(MyUrl);
};
};
The problem is when I do the above I only get the result of first loop element (i=0)
in the console.log(AllLinks)
. How do I properly loop in node? I am new to node so any comments are much appreciated!
EDIT:
If I define AllLinks
in request
then it seems to work, but not in correct order...
var third = function(classes){
for (var i = 0; i <= (classes.length-1); i++) {
var Myurl = classes[i];
(function(Myurl){
request(Myurl, function(err, resp, body) {
if (!err && resp.statusCode == 200) {
var $ = cheerio.load(body);
AllLinks=[];
$("#item_details dl").each(function() {
var Values = [];
var New = [];
Values=$(this).find("dd strong").text();
New = Values.replace(/[\n\t\r]/g,"");
AllLinks.push(New);
});
console.log(AllLinks);
}(Myurl);
})
};
};
};