 <p>You can use the async package which is great for controlling flow, something like:</p> <pre><code> console.log('Recieved the get Request'); var i = 1; var count = 0; while (count &lt; 10) { var url = 'http://www.imdb.com/title/tt' + i + '/'; console.log(url); count = count + 1; i = i + 1; async.waterfall([ function sendRequest (callback) { if (!error) { var $ = cheero.load(html); var json = { title: '', ratings: '', released: '' } } $('.title_wrapper').filter(function() { var data = $(this); json.title = data.children().first().text().trim(); json.released = data.children().last().children().last().text().trim(); }); $('.ratingValue').filter(function() { var data = $(this); json.ratings = parseFloat(data.text().trim()); }); callback(null, JSON.stringify(json, null, 4) + '\n'); }, function appendFile (json, callback) { fs.appendFile('message.txt', json, function(err) { if (err) { callback(err); } callback(); }); } ], function(err) { res.sendFile(__dirname + '/index.js'); }); </code></pre>