如何从承诺中获取价值?

时间:2020-01-02 16:37:43

标签: node.js for-loop promise request-promise

for (var year = year_from; year <= year_to; year++) {
  for (var month = month_from; month <= 12; month++) {
    for (var day = day_from; day <= 31; day++) {
      var url = "something_url" + year + "/" + month + "/" + day + "/";
      var current_date = day + "." + month + "." + year;
      try {
        var options = {
          uri: url,
          transform: function (body) {
            var $ = cheerio.load(body);
            $('div[class="post"]').each(function (month, elem) {
              var nested = $(this).find('div[class="text"]');
              var nested_a = nested.find('a');
              var nested_href = nested_a.attr('href');
              var nested_text = nested_a.text();
              var lc_nested_text = nested_text.toLowerCase();
              var s_lc_nested_text = lc_nested_text.toString();
              if (s_lc_nested_text.indexOf(query1) > -1) {
                post.name1 = s_lc_nested_text;
                post.url1 = f_url + nested_href;
                post.date1 = current_date;
              }
            });
          }
        };

        prs.push(rp(options));
      }
      catch {
        console.log('parse error');
      }
    }
  }
}

Promise.all(prs)
  .then((result) => {
    res.send(result);
  }).catch(err => console.log(err));  

我以请求承诺的方式将请求的结果从API发送到API,并将其放入承诺中。将结果发送到浏览器后,我得到以下信息:

Browser Response

0 个答案:

没有答案