I am trying to download a web-page using nodejs, javascript. However it seems to have a inifinite loop. Why?
var downloadFinished = false;
var downloadPage = function(url,file) {
rest.get(url).on('complete', function(result) {
if (result instanceof Error) {
console.log('Error:', result.message);
} else {
fs.writeFileSync(file, result, 'utf8');
downloadFinished = true;
}
});
};
if(require.main == module) {
downloadPage('http://google.com', 'new.html');
while(!downloadFinished) {
// wait till download finished.
}
// do other stuff make sure download is finished to 'new.html'
}
successcallback when using jquery's$.ajax(api.jquery.com/jQuery.ajax)