I am trying to periodically load some data from external API like this:
setInterval(function() {
getData();
}, 60000);
function getData() {
if (typeof someObject.data === 'object') {
for (var prop in someObject.data) {
if (prop === 1 || prop === 2) {
var options = {
host: 'somehost.com',
path: '/somepath?param=' + prop
};
var req = http.request(options, function(res) {
// EXECUTION NEVER REACHES THIS POINT ?!?!
req.on('end', function() { alert('ended'); });
});
req.end();
}
}
}
}
If I do not do any intervals and loops, such request to the same host works perfectly. However, if I try to do something like shown above, then the request never ever calls its callback function.
What I am doing wrong here?
typeof someObject === 'object'as well, I just didn't include it here for better readability. It reaches the line where the request is made but it never returns. If I remove the other stuff (setInterval and the loop), everything executes perfectly.