Return .each 完成时

Return when .each is completed

Promise.try(function (){
    return Promise.all(splitup); // [Show, me, stocks, for, Google, and, Microsoft]
}).each(function (item) { // Looping through entities, [Show] [me] [stocks] ...
    alchemyapi.entities('text', item, { 'sentiment' : 1}, function (response) {
        if(response.entities) { // An entity is found, ex. Microsoft
            if(response.entities[0].type === "Company") {
                requestBody.push(item);
                console.log("Item was found, added " + item);
            } else {
                console.log(item + " Is not a company");
            }
        } else { // no entity found for that one word
            console.log("No entity found for " + item);
        }
    });
}).then(function (response) {
    // send requestBody when loop is completed.
});

我从 return 字符串数组 splitup 开始,这样我就可以遍历 line 3 上的每个元素。

假设 splitup 数组如下所示:[AppleAndMexico]

Apple 是一家公司,所以 if(response.entities) return 是正确的,然后它会检查 JSON 响应以查看它是否是一家公司,该声明 returns true 并将其添加到我正在构建的新 requestBody 数组中。

接下来,单词 'And' return 在 if(response.entities) 上为 false 所以它转到 else 语句。

接下来,让我们选择 Mexicoif(response.entities) 为 return,if(response.entities[0].type === "Company")

为 return 为假

我的问题是,我想 return 新的 requestBody 数组,当它完成对每个项目的循环时,但我不完全确定如何判断循环何时完成,并且什么时候 return requestBody

您需要使用 Promise.filter 而不是 Promise.each。 Promise.filter 使用传递给它的过滤器函数将给定数组过滤到另一个数组。

因此,当您遇到一家公司 ('Apple') 时,您会 resolve 它的价值,如果它有其他任何东西 ('Mexico' 和 'And'),您会用 false.

Promise.filter(splitup, function (item) { // Looping through entities, [Show] [me] [stocks] ...
    return new Promise(function(resolve, reject) {
        alchemyapi.entities('text', item, { 'sentiment' : 1}, function (response) {
            if(response.entities) { // An entity is found, ex. Microsoft
                if(response.entities[0].type === "Company") {
                    console.log("Item was found, added " + item);
                    return resolve(item);
                } else {
                    console.log(item + " Is not a company");
                    return reject(false);
                }
            } else { // no entity found for that one word
                console.log("No entity found for " + item);
                return reject(false);
            }
        });
    });
}).then(function (requestBody) {
    // send requestBody when loop is completed.
});

好的太晚了:)。这是我的结果:

var alchemyapi = require('alchemy-api');
var Promise = require('bluebird');
var alchemyapi = new alchemyapi(<YOUR_KEY>);

var test = ['Microsoft', 'and', 'Apple'];

Promise.filter(test, function(item) {
  return getCompanyName(item).then(function(){
    return true;
  }, function(reason) {
    console.log(reason.message);
    return false;
  });
}).then(function(requestBody){
  console.log(requestBody);
});

function getCompanyName(item) {
  return new Promise(function(resolve, reject) {
    alchemyapi.entities(item, {sentiment: 1}, function (err, response) {
      if (err) reject(err);
      if (response.entities.length > 0) { // An entity is found, ex. Microsoft
        if (response.entities[0].type === "Company") {
          resolve(item);
        } else {
          reject(new Error(item + " Is not a company"));
        }
      } else { // no entity found for that one word
        reject(new Error("No entity found for " + item));
      }
    });
  });
}