Skip to content

Instantly share code, notes, and snippets.

@podkopaev
Created January 13, 2014 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save podkopaev/8406346 to your computer and use it in GitHub Desktop.
Save podkopaev/8406346 to your computer and use it in GitHub Desktop.
The problem is that this function returns an empty array. The array im talking about is hoster.
portal.getHosterByStream = function(stream, callback) {
request(URL + stream, function (error, response, body) {
if (!error && response.statusCode === 200) {
var hoster = [];
var $ = cheerio.load(body);
var information = $('body div[id=frmMain] div[id=dontbeevil] div[id=Vadda]').html();
var items_hoster = $(information).find("ul[id=HosterList]").html();
$(items_hoster).each(function (i, item) {
var rel = $(item).attr('rel');
if (rel != undefined) {
request(MIRROR + rel, function (error, response, body) {
if (!error && response.statusCode === 200) {
var host = JSON.parse(body);
var href = "";
var positionHref = 9;
var i = 0;
while (host.Stream.substr(positionHref + i, 1) != '"') {
href = util.format('%s%s', href, host.Stream.substr(positionHref + i, 1));
i++;
}
hoster.push(href);
} else {
console.log('error second request');
}
});
}
});
callback(hoster);
} else {
console.log('error request page');
}
});
}
@st-fankl-in
Copy link

Not tested, but i think i got this problem before.
The each at Line 9 returns imidiately, so the callback Line 33 get executed before the array is filed.
You might have a look at array filter/map (and/or take the nodeschool.io functional programming lesson).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment