简体   繁体   中英

How to write node.js Async inside for loop?

I started build nodejs app to scrape data from a site that has pagination. I write for loop to get all page and push all url to store in an array. Inside that for loop I write async.eachSeries to run all url one by one and after it finish running I want to print out success message. But I don't know how to put that together to make it works properly. Here is my code

for(var pageNo = 1; pageNo <=200; pageNo++){
  siterequest('https://www...../en/jobs/?by=&in=&page='+pageNo, function(err, res, body){
    if(!err && res.statusCode ==200){
        var $ = cheerio.load(body);
        var links = [];
        $('.headline3.job-name-title > strong > a').each(function(i, elem){
            links.push('https://www......com.kh/'+$(elem).attr('href'));
        });
        async.eachSeries(links, function(uri, next){
            console.log('i will go this '+uri);
            next();
        }, function(callback){
            console.log('I did it');
        });
      };
  });
};

The code above did not work for me. Please help me! Thanks in advance

As you are using async.js you can try async.times instead of for loop

async.times(200, function (paneNo, nextPage) {
  siterequest('https://www...../en/jobs/?by=&in=&page=' + (pageNo + 1), 
    function (err, res, body) {
      if (!err && res.statusCode ==200) {
        var $ = cheerio.load(body);
        var links = [];

        $('.headline3.job-name-title > strong > a').each(function (i, elem) {
          links.push('https://www......com.kh/'+$(elem).attr('href'));
        });

        async.eachSeries(links, function (uri, next) {
          console.log('i will go this '+ uri);
          next();
        }, function(callback) {
          nextPage();
        });
      } else {
        nextPage(err);
      }
  });
}, function(err) {
  console.log('Done!');
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM