私は 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