Delay of each loop iteration in node js, async

I have the code below:

var request = require('request'); var cheerio = require ("cheerio"); var async= require("async"); var MyLink="www.mylink.com"; async.series([ function(callback){ request(Mylink, function (error, response, body) { if (error) return callback(error); var $ = cheerio.load(body); //Some calculations where I get NewUrl variable... TheUrl=NewUrl; callback(); }); }, function(callback){ for (var i = 0; i <=TheUrl.length-1; i++) { var url = 'www.myurl.com='+TheUrl[i]; request(url, function(error, resp, body) { if (error) return callback(error); var $ = cheerio.load(body); //Some calculations again... callback(); }); }; } ], function(error){ if (error) return next(error); }); 

Does anyone have a suggestion on how I can delay every loop iteration in for loop ? Say the code waits 10 seconds after the completion of each iteration. I tried setTimeout but could not do it.

+7
javascript for-loop
source share
3 answers

You can set a timeout to execute code at increasing intervals as follows:

 var interval = 10 * 1000; // 10 seconds; for (var i = 0; i <=TheUrl.length-1; i++) { setTimeout( function (i) { var url = 'www.myurl.com='+TheUrl[i]; request(url, function(error, resp, body) { if (error) return callback(error); var $ = cheerio.load(body); //Some calculations again... callback(); }); }, interval * i, i); } 

So, the first starts immediately (the interval * 0 is 0), the second - after ten seconds, etc.

You need to send i as the final parameter to setTimeout() so that its value is bound to the function argument. Otherwise, an attempt to access the value of the array will be out of bounds, and you will get undefined .

+11
source share

Another alternative would be to use async.eachSeries . For example:

 async.eachSeries(TheUrl, function (eachUrl, done) { setTimeout(function () { var url = 'www.myurl.com='+eachUrl; request(url, function(error, resp, body) { if (error) return callback(error); var $ = cheerio.load(body); //Some calculations again... done(); }); }, 10000); }, function (err) { if (!err) callback(); }); 
+11
source share

Since you are already using async , async.wilst will be nicely used to replace for .

whilst is an asynchronous while function. Each iteration starts only after the previous iteration called the completion callback. In this case, we can simply delay execution of the completion callback for 10 seconds using setTimeout .

 var i = 0; async.whilst( // test to perform next iteration function() { return i <= TheUrl.length-1; }, // iterated function // call `innerCallback` when the iteration is done function(innerCallback) { var url = 'www.myurl.com='+TheUrl[i]; request(url, function(error, resp, body) { if (error) return innerCallback(error); var $ = cheerio.load(body); //Some calculations again... // wait 10 secs to run the next iteration setTimeout(function() { i++; innerCallback(); }, 10000); }); }, // when all iterations are done, call `callback` callback ); 
+9
source share

All Articles