nodejs在内部使用异步函数进行循环

时间:2018-02-15 21:35:33

标签: javascript node.js loops asynchronous

我遇到了for(var x=1; x < 6; x++)被调用的问题,因为太快axios.get()是异步的,但我不知道如何在没有解决方案太复杂的情况下解决这个问题

const axios = require("axios");
const cheerio = require("cheerio");

function imdbGetData(id) {
  var title, show, $;
  var arr = [];
  var airdates = [];
  show = {
    seasons: []
  };

  axios.get(`http://www.imdb.com/title/${id}/`).then((body) => {
    $ = cheerio.load(body.data);
    title = $("div h1").text()
  });
  for(var x=1; x < 6; x++) {
    console.log(x); // Will count too 1,2,3,4,5,6
    url = `http://www.imdb.com/title/${id}/episodes?season=${x}`
    axios.get(url).then((body) => {
      $ = cheerio.load(body.data);
      console.log(x);// 6, 6, 6, 6
      $("div .info .airdate").each(function(index, item) {
        var airdate = String($(this).text());
        airdates.push(airdate.trim());
      });


      $(".info strong a").each(function(i, item){
          var airdate = airdates[i];

          var epsiode_name = $(this).text()
          if (epsiode_name && !epsiode_name.includes("#"))
            arr.push({epsiode_name, airdate});
      });
      show.seasons.push(arr);
      arr = []
      // console.log(show.seasons);
    });
    setTimeout(() => {console.log(show.seasons)}, 10000) // ghetto
  }
}

// season = {
//   seasons: [[ {epsiode_name} ], [{Epsiode name}]]
// }

imdbGetData("tt2193021");

3 个答案:

答案 0 :(得分:4)

您可以构造并将所有promises推送到数组,然后使用Promise.all(arrayOfPromises)。通过这种方式,您将保留异步链,并且可以轻松处理与常规单个异步操作非常相似的结果:

var promises = [];
for (var x = 1; x < 6; x++) {
  url = `http://www.imdb.com/title/${id}/episodes?season=${x}`
  promises.push(axios.get(url));
}

Promise.all(promises)
  .then(body => {
    // all results of promises will be in 'body' parameter
  })
  .catch(err => console.error(err));

答案 1 :(得分:0)

您还可以使用async / await(在较新版本的Node.js中),这样您可以使代码更容易阅读,我也做了一些小改动来更新进度。

const axios = require("axios");
const cheerio = require("cheerio");

async function imdbGetData(id) {

    var title, show, $;
    var arr = [];
    var airdates = [];
    show = {
    seasons: []
    };

    console.log('Getting from ' + `http://www.imdb.com/title/${id}/`);
    let body = await axios.get(`http://www.imdb.com/title/${id}/`);

    $ = cheerio.load(body.data);
    title = $("div h1").text()

    for(var x=1; x < 6; x++) {
        console.log('Getting season: ' + x); // Will count too 1,2,3,4,5,6
        url = `http://www.imdb.com/title/${id}/episodes?season=${x}`
        let body = await axios.get(url);
        $ = cheerio.load(body.data);
        $("div .info .airdate").each(function(index, item) {
            var airdate = String($(this).text());
            airdates.push(airdate.trim());
        });

        $(".info strong a").each(function(i, item){
            var airdate = airdates[i];

            var epsiode_name = $(this).text()
            if (epsiode_name && !epsiode_name.includes("#"))
                arr.push({epsiode_name, airdate});
        });
        show.seasons.push(arr);
        arr = []

    }

    console.log("Result: ", show.seasons);
}

imdbGetData("tt2193021");

答案 2 :(得分:0)

您可以简单地使用ES6 let而不是var,您的代码将是:

for(let i=0; i<length; i++){
   asyncCall(function(){
    console.log(i);// will print 0,1,2,3,...
    });
}

请查看这篇文章https://codeburst.io/asynchronous-code-inside-an-array-loop-c5d704006c99