简体   繁体   English

闭包,循环和承诺

[英]Closures, loops, and promises

I've read several posts on the subject, but unfortunately struggling with my situation. 我已经阅读了有关该主题的几篇文章,但是很遗憾,我的处境艰难。

I am pulling some urls from an array of urls and then using those links to obtain (let's say) the title of the page from those subsites. 我从一组URL中提取一些URL,然后使用这些链接从那些子站点获取(例如)页面的标题。 At the end I want a list of original urls and an array of titles (from the sublinks). 最后,我想要一个原始URL列表和一个标题数组(来自子链接)。 ie, go into a site/domain, find some links, curl those links to find page titles. 即,进入站点/域,找到一些链接,卷曲那些链接以找到页面标题。

Problem is that my titleArray just returns a Promise and not the actual data. 问题是我的titleArray仅返回Promise而不是实际数据。 I'm totally not getting closures right and promises. 我完全没有实现正确的关闭和承诺。 Code runs in node as is. 代码按原样在节点中运行。 I'm using personal sites in my real code, but substituted common sites to show an example. 我在真实代码中使用个人网站,但替换了常见网站以显示示例。

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

}

The result after running this is: 运行此后的结果是:

http://www.google.com: [object Promise]
http://www.cnn.com: [object Promise]
The Title for http://www.espn.com
The Title for http://www.espn.com
The Title for http://www.yahoo.com
The Title for http://www.yahoo.com

whereas it should be: 而应该是:

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']
...

You cannot return normal data from inside a Promise . 您不能从Promise内部返回普通数据。 You need to return another Promise to make it chainable. 您需要返回另一个Promise以使其可链接。 To process multiple Promise objects in loop, you need to push them in an array and call Promise.all(array) ; 要循环处理多个Promise对象,您需要将它们推入数组并调用Promise.all(array) ;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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