admin管理员组文章数量:1415460
I use node.js on my server and I use redis key-store for storing data about my characters. Every connection has it own character. I want to get all data about characters(Person, has its name, age, profession, ...) into Characters array, so I can then selectively send it to connected clients.
var Characters = [];
for (var ID in Connections) {
redis_client.HGETALL(ID, function(err, result) {
if (result) {
Characters.push(result);
}
});
}
console.log(Characters);
I have read, that this is due to asynchronous vs synchronous problem, so I made global variable character.
//global variables
var character;
//function code
var Characters = [];
for (var ID in Connections) {
redis_client.HGETALL(ID, function(err, result) {
character = result;
});
if(character) {
console.log(character); // returns correct result
// make copy of character
Characters.push(JSON.parse(JSON.stringify(character)));
character = undefined;
}
}
console.log(Characters); // array of 1 character * number of connection
//BUT I expect different character for each connection
I use node.js on my server and I use redis key-store for storing data about my characters. Every connection has it own character. I want to get all data about characters(Person, has its name, age, profession, ...) into Characters array, so I can then selectively send it to connected clients.
var Characters = [];
for (var ID in Connections) {
redis_client.HGETALL(ID, function(err, result) {
if (result) {
Characters.push(result);
}
});
}
console.log(Characters);
I have read, that this is due to asynchronous vs synchronous problem, so I made global variable character.
//global variables
var character;
//function code
var Characters = [];
for (var ID in Connections) {
redis_client.HGETALL(ID, function(err, result) {
character = result;
});
if(character) {
console.log(character); // returns correct result
// make copy of character
Characters.push(JSON.parse(JSON.stringify(character)));
character = undefined;
}
}
console.log(Characters); // array of 1 character * number of connection
//BUT I expect different character for each connection
Share
Improve this question
asked Jun 21, 2014 at 12:17
James07James07
2932 gold badges5 silver badges14 bronze badges
2 Answers
Reset to default 1there are different ways,
the easiest way would be creating calling the async function one after another, as follows
var Characters = [];
var objectKeys = Object.keys(Connections);
var ID = 0;
if (ID < objectKeys.length)
doCall(objectKeys[ID]);
else
console.log(Characters);
function doCall(key) {
redis_client.HGETALL(key, function(err, result) {
if (result) {
Characters.push(result);
}
ID++;
if ( ID < objectKeys.length)
doCall(objectKeys[ID]);
else
console.log(Characters);
});
}
Regular for loop doesn't work correctly when you have async call and global variables. Here is a loop version that would work (make sure async is installed. if not do "npm install async"):
var async = require('async');
var Characters = [];
async.each(Connections, function(ID, callback) {
redis_client.HGETALL(ID, function(err, result) {
if (result) {
Characters.push(result);
}
callback(null);
});
}, function(err) { // this function gets called when the loop is done
console.log("finish:");
// print out your array
for (var i = 0, len = Characters.length; i < len; i++) {
console.log(Characters[i]);
}
});
本文标签: javascriptPush object into local array from asynchronous callback functionStack Overflow
版权声明:本文标题:javascript - Push object into local array from asynchronous callback function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745175654a2646225.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论