Javascript 闭包、循环和承诺

Javascript 闭包、循环和承诺,javascript,node.js,promise,closures,Javascript,Node.js,Promise,Closures,我已经读了好几篇关于这个主题的帖子,但不幸的是,我的处境很艰难 我从URL数组中提取一些URL,然后使用这些链接从这些子网站获取(比如)页面标题。最后,我想要一个原始URL列表和一个标题数组(来自子链接)。i、 例如,进入一个站点/域,找到一些链接,卷曲这些链接以找到页面标题 问题是我的titleArray只返回一个承诺,而不是实际数据。我完全没有得到正确的结论和承诺。代码按原样在节点中运行。我在我的真实代码中使用了个人网站,但用普通网站来展示一个例子 const popsicle = requ

我已经读了好几篇关于这个主题的帖子,但不幸的是,我的处境很艰难

我从URL数组中提取一些URL,然后使用这些链接从这些子网站获取(比如)页面标题。最后,我想要一个原始URL列表和一个标题数组(来自子链接)。i、 例如,进入一个站点/域,找到一些链接,卷曲这些链接以找到页面标题

问题是我的titleArray只返回一个承诺,而不是实际数据。我完全没有得到正确的结论和承诺。代码按原样在节点中运行。我在我的真实代码中使用了个人网站,但用普通网站来展示一个例子

const popsicle = require('popsicle');

var sites = ['http://www.google.com','http://www.cnn.com'];

// loop through my sites (here I'm just using google and cnn as a test
for(var i=0;i<sites.length;i++) {
  // call function to pull subsites and get titles from them
  var titleArray = processSites(sites[i]);

  console.log(sites[i] + ": " + titleArray);
}

// get request on site and then get subsites
function processSites(url) {
  return popsicle.get(url)
    .then(function(res) {
      var data = res.body;

      // let's assume I get another collection of URLs
      // that I pull from the main site
      var subUrls = ['http://www.yahoo.com','http://www.espn.com'];
      var titleArray = [];
      for(var j=0;j<subUrls.length;i=j++) {
        var title = processSubSites(subUrls[j])
        titleArray.push(title);
      }
      return titleArray;
    });
}

function processSubSites(url) {

  return popsicle.get(url)
    .then(function(res) {
      var data = res.body;
      // let's say I pull the title of the site somehow
      var title = "The Title for " + url;
      console.log(title);
      return title;
    });

}
鉴于它应该是:

http://www.google.com: ['The Title for http://www.yahoo.com', 'The Title for http://www.espn.com']
http://www.cnn.com: ['The Title for http://www.yahoo.com', 'The Title for http://www.espn.com']
...

无法从
承诺
中返回正常数据。您需要返回另一个
Promise
以使其可链接。要在循环中处理多个
Promise
对象,您需要将它们放入一个数组中并调用
Promise.all(array)

const冰棒=require(“冰棒”);
var站点=['http://www.google.com','http://www.cnn.com'];
var titleArrayPromises=[];
//循环浏览我的网站(这里我只是使用谷歌和cnn作为测试)

对于(var i=0;i1.yahoo.com和espn.com是mainURL的子URL吗?我可以建议您使用mainURL及其相应的子URL修改您的问题/示例,然后修改实际/预期结果吗?2.您是否正在为子URL或主URL提取标题您正在使用的nodejs版本。如果是>=7.6 yo你可以利用async和wait-@Jaya-抱歉,我在这里没有使用一个好的示例。实际上,主URL或域级别和子URL都在域内。我只是从子URL中提取标题。@codetext-我现在在节点7.0,但我将研究async和wait。
http://www.google.com: ['The Title for http://www.yahoo.com', 'The Title for http://www.espn.com']
http://www.cnn.com: ['The Title for http://www.yahoo.com', 'The Title for http://www.espn.com']
...
const popsicle = require('popsicle');

var sites = ['http://www.google.com','http://www.cnn.com'];

var titleArrayPromises = [];
// loop through my sites (here I'm just using google and cnn as a test
for(var i=0;i<sites.length;i++) {
  titleArrayPromises.push(processSites(sites[i]));
}

var titleArray = [];
Promise.all(titleArrayPromises).then(function (titleArrays) {
  for(var i=0; i<titleArrays.length; i++) {
    titleArray.concat(titleArrays[i])
  }
  // You now have all the titles from your site list in the titleArray
})

function processSites(url) {
  return popsicle.get(url)
    .then(function(res) {
      var data = res.body;

      // let's assume I get another collection of URLs
      // that I pull from the main site
      var subUrls = ['http://www.yahoo.com','http://www.espn.com'];
      var titlePromiseArray = [];
      for(var j=0;j<subUrls.length;j++) {
        titlePromiseArray.push(processSubSites(subUrls[j]));
      }
      return Promise.all(titlePromiseArray);
    });
}

function processSubSites(url) {
  return popsicle.get(url)
    .then(function(res) {
      var data = res.body;
      // let's say I pull the title of the site somehow
      var title = "The Title for " + url;
      return Promise.resolve(title);
    });  
}