-2
users = [];
.on('result', function(user) {
async.waterfall([
function(cb) {
get_comm(user.post_id, function(comms) {
user.comm = comms;
users.push(user);
cb(users);
})
},
], function(users, cb) {
console.log(users); // it prints out
});
})
.on('end', function() {
if (connectionsArray.length) {
pollingTimer = setTimeout(pollingLoop, POLLING_INTERVAL);
updateSockets({
users: users // empty []; !!
});
}
});
所以我想在結果循環完成後打印出所有的數據,但數組在結果循環中已滿並空出它。Nodejs回調異步
var get_comm = function(post_id,cb){varcomments = connection.query(..); comments.on('end',function(){ cb(comms);})} users = []; var query = connection.query(...); 。對( '結果',功能(用戶){ async.waterfall([ 函數(CB){get_comm(user.post_id,函數(通訊){ user.comm =通訊科; users.push(用戶); CB(用戶); }) }, ],功能(用戶,CB){ 的console.log(用戶); //它打印出 }); }) 。對( 'end',函數()if(connectionsArray.length){pollingTimer = setTimeout(pollingLoop,POLLING_INTERVAL); updateSockets({ rs:users // empty []; ! }); } }); – 2014-11-20 22:38:39
這裏是完整的代碼檢查@ jfriend00 – 2014-11-20 23:18:11