Delay each loop iteration in node js, async

后端 未结 4 1020
一个人的身影
一个人的身影 2020-12-14 19:54

I have the code below:

var request = require(\'request\');
var cheerio = require (\"cheerio\");
var async= require(\"async\");

var MyLink=\"www.mylink.com\"         


        
4条回答
  •  别那么骄傲
    2020-12-14 20:13

    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();
    });
    

提交回复
热议问题