0

我对 node.js 还很陌生,我不知道如何控制程序流,以便我的函数等待带有内部回调的 Underscore _.each() 块。我真的很想避免令人发指的回调炖菜。该块位于已由 nimble 的 .series([]) 控制的链中

function (callback) { //get common friends    
    _.each(User.friends, function (friend) { //I NEED THE FLOW TO WAIT TIL THIS COLLECTION HAS ITERATED AND ALL THE CALLBACKS ARE COMPLETE
        request.get({
            url: 'https://graph.facebook.com/me/mutualfriends/' + friend.id + '?access_token=' + User.accessToken,
            json: true
        }, function (error, response, body) { //NEED TO WAIT TIL THESE ARE ALL COMPLETED
            if (!error && response.statusCode == 200) {
                console.log("common friends", body.data);

            } else {
                console.log(error);
            }
        });

    }, this);

    callback(); //Nimble's serialization callback fires immediately
},

我在下面尝试了使用 async.each 的建议,但我无法触发迭代完成回调,以告诉 nimble 的 .series 函数继续到下一个块。

async.each(User.friends, function(friend) {
        request.get({
        url: 'https://graph.facebook.com/me/mutualfriends/'+friend.id+'?access_token=' + User.accessToken,
        json: true
        }, function (error, response, body) { //NEED TO WAIT TIL THESE ARE ALL COMPLETED
            if (!error && response.statusCode == 200) {
                console.log("common friends",body.data);

            } else {
                console.log(error);
            }
        });


    },function(err) {console.log('Final callback');callback();}); //THIS CALLBACK DOESN'T FIRE - NIMBLE JUST SITS AND WAITS
4

3 回答 3

2

您可以async为此使用该模块。

async.each

您的代码应如下所示:

async.each(User.friends, function(friend, cb) {
  var req = {
    url: 'https://graph.facebook.com/me/mutualfriends/'+friend.id+
      '?access_token='+User.accessToken,
    json: true
  };
  request.get(req, function(err,response,body) {
    if(err) { console.log(err); cb(true); return; }
    console.log("common friends",body.data);
    // each function call has to finish by calling `cb`
    cb(false);
  });
},
function(err) {
  if(err) return;
  console.log('Final callback');
  callback(); // here is your final callback
}
);
于 2013-06-10T02:07:36.743 回答
1

试试这个。

function (callback) { //get common friends  
 var completeCount = 0;  
  _.each(User.friends, function (friend) { 
    request.get({
        url: 'https://graph.facebook.com/me/mutualfriends/' + friend.id + '?access_token=' + User.accessToken,
        json: true
    }, function (error, response, body) { //NEED TO WAIT TIL THESE ARE ALL COMPLETED
        if (!error && response.statusCode == 200) {
            console.log("common friends", body.data);

        } else {
            console.log(error);
        }

        completeCount++;

        // complete count check           
        if( completeCount === User.friends.length ){
           callback()
        }
    });

  }, this);    
},
于 2013-06-10T06:52:12.047 回答
1

您的代码几乎是正确的。您必须传递并使用回调函数,否则 Async 不知道何时调用最终回调。

async.each(User.friends, function(friend, cb) {
    request.get({
        url: 'https://graph.facebook.com/me/mutualfriends/' + friend.id + '?access_token=' + User.accessToken,
        json: true
    }, function(error, response, body) { //NEED TO WAIT TIL THESE ARE ALL COMPLETED
        if (!error && response.statusCode == 200) {
            console.log("common friends", body.data);
            cb(null);
        } else {
            console.log(error);
            callback(error);
        }
    });
}, function(err) {
    console.log('Final callback', err);
    callback();
});
于 2013-06-10T09:21:17.847 回答