每个.each完成后返回

时间:2015-12-03 22:20:18

标签: arrays node.js promise bluebird

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

我首先返回一个字符串数组splitup,这样我就可以遍历line 3上的每个元素。

让我们说splitup数组如下:[AppleAndMexico]

Apple是公司,因此if(response.entities)返回true,然后检查JSON响应以查看它是否是公司,该语句返回true并添加它到我建立的新requestBody阵列。

接下来,单词'And'if(response.entities)上返回false,因此它转到else语句。

接下来,让我们选择Mexico,它会在if(response.entities)返回true,但在if(response.entities[0].type === "Company")

返回false

我的问题是,我想在完成循环遍历每个项目后返回新的requestBody数组,但我不完全确定循环完成时我怎么知道,以及何时返回requestBody

2 个答案:

答案 0 :(得分:1)

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

因此,当您遇到公司('Apple')时,您resolve的价值,如果是其他任何内容('墨西哥'和'和'),您可以使用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.
});

答案 1 :(得分:0)

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

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