문제

How do I delay a promise until an asynchronous operation has completed? I am using async and bluebird libraries. As soon as I boot up my program, the done() function returns an error that is an empty or nearly empty 'masterlist' object. Why isn't async waiting until the iterator has finished its operation?

// bundler.js

var masterlist = {
   "children": []
 , "keywords": []
 , "mentions": 0
 , "name" : "newsfeed"
 , "size" : 0
}

// initialize() returns a promise with the populated masterlist
exports.initialize = function() {
  return new Promise(function(resolve, reject) {

    // pullBreakingNews() returns a promise with the breaking news articles
    nytimes.pullBreakingNews().then(function(abstracts) {

      async.map(abstracts, iterator, done);

      function iterator(item, callback) {
        alchemyapi.entities('text', item, {}, function(response) {

          // initialize each entity with masterlist
          response.entities.forEach(function(entity) {
            masterlist.children[masterlist.children.length] =
             {
                 "abstract": item
               , "children": []
               , "name": entity.text
               , "size": 0
             };
            masterlist.size += 1;
            masterlist.keywords.push(entity.text);
          });

          callback(masterlist);
        });
      };

      function done(err, results) {
        if (err) {
          console.log("ERROR: ", err);
        } else {
          resolve(results);
        }
      };

    });
  });
};

Firehose.js is the module that calls initializer(). I believe that firehose gets run first, and the promise is called in the process. server.js => firehose.js => bundler.js => nytimes api

// firehose.js
// Compares entities to Twitter stream, counts every match
exports.aggregator = function(callback) {
  bundler.initialize().then(function(masterlist) {

    t.stream('statuses/filter', { track: masterlist.keywords }, function(stream) {

      // read twitter firehose for incoming tweets.
      stream.on('data', function(tweet) {
        var tweetText = tweet.text.toLowerCase();

        // sift through each tweet for presence of entities
        masterlist.children.forEach(function(parentObject) {

          // if the entity exists in the tweet, update counters
          if (tweetText.indexOf(parentObject.name.toLowerCase()) !== -1) {
            parentObject.size += 1;
            masterlist.mentions += 1;
            callback(masterlist);
          }

        });
      });
    });
  });
};

Thanks very much for any help.

도움이 되었습니까?

해결책 2

The iterator's callback accepts an error as the first argument. You should pass a falsy value (like a null) there instead of masterlist if there's no error.

function iterator(item, callback) {
    alchemyapi.entities('text', item, {}, function(response) {

      // initialize each entity with masterlist
      response.entities.forEach(function(entity) {
        masterlist.children[masterlist.children.length] =
         {
             "abstract": item
           , "children": []
       , "name": entity.text
       , "size": 0
         };
        masterlist.size += 1;
        masterlist.keywords.push(entity.text);
      });

      callback(null, masterlist);
    });
  };

다른 팁

Please don't mix callbacks and promises, only use either one.

// Do this somewhere else, it is only needed once
// it adds promise returning versions of all alchemy methods for you
Promise.promisifyAll(require('alchemy-api').prototype);

exports.initialize = function() {
  return nytimes.pullBreakingNews().map(function(abstract) {
    // Note that it is entitiesAsync that is a promise returning function 
    return alchemyapi.entitiesAsync('text', abstract, {}).then(function(response){
      response.entities.forEach(function(entity) {
            masterlist.children[masterlist.children.length] =
             {
                 "abstract": abstract
               , "children": []
               , "name": entity.text
               , "size": 0
             };
            masterlist.size += 1;
            masterlist.keywords.push(entity.text);
      });
    });
  }).return(masterlist);
};

Also your initialize function isn't checking if it is initialized already

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top