Push object into local array from asynchronous callback function

James07

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
Kamrul

there 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);
        });
   }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Push to array from callback

From Dev

Calling function, from callback stored in object, in an array

From Javascript

How to return value from an asynchronous callback function?

From Dev

Access correct "this" from callback function in array of functions inside an object

From Dev

Node object assignment in asynchronous callback function hanging indefinitely

From Dev

php push to array from function

From Dev

Is there a way to push an object to an array from an object? (Vue)

From Dev

Asynchronous Callback to Array.map()

From Dev

How to push an object into an array in async function

From Dev

Reduce callback function to retrieve properties from an array

From Dev

Refractroing: return or push value to new array value from mongoose callback

From Dev

Is it possible to affect local variables from a callback function in C++?

From Dev

How to push object from oner array to another?

From Dev

Push object from rest api into an array Angular

From Dev

get values from object and push into array javascript

From Dev

Push value from Observable<Object> into Observable<Array>

From Dev

Dart callback - passing asynchronous message to parent object

From Dev

How I make parse function to create var array object from local file?

From Dev

returning an object from an android ViewModel to the fragment from a callback function

From Dev

How to specify callback function when filtering array with object elements in php?

From Dev

Add A Callback To A Prebuilt Asynchronous Function Swift iOS

From Dev

How to store the variable of an asynchronous callback function

From Dev

DeprecationWarning: Calling an asynchronous function without callback is deprecated

From Dev

Iterate over the entries of a Map with an asynchronous callback function

From Dev

Meteor: How to catch asynchronous callback function errors

From Dev

Retrieving values in an object in an array from local storage

From Dev

How to write a callback function for asynchronous calls from helper method to componentDidMount using React JS

From Dev

How to pause an asynchronous Swift function until a callback is called when a result is available from another library(JavaScriptCore)

From Dev

showing empty array in after pushing data from foreach into it in asynchronous function

Related Related

  1. 1

    Push to array from callback

  2. 2

    Calling function, from callback stored in object, in an array

  3. 3

    How to return value from an asynchronous callback function?

  4. 4

    Access correct "this" from callback function in array of functions inside an object

  5. 5

    Node object assignment in asynchronous callback function hanging indefinitely

  6. 6

    php push to array from function

  7. 7

    Is there a way to push an object to an array from an object? (Vue)

  8. 8

    Asynchronous Callback to Array.map()

  9. 9

    How to push an object into an array in async function

  10. 10

    Reduce callback function to retrieve properties from an array

  11. 11

    Refractroing: return or push value to new array value from mongoose callback

  12. 12

    Is it possible to affect local variables from a callback function in C++?

  13. 13

    How to push object from oner array to another?

  14. 14

    Push object from rest api into an array Angular

  15. 15

    get values from object and push into array javascript

  16. 16

    Push value from Observable<Object> into Observable<Array>

  17. 17

    Dart callback - passing asynchronous message to parent object

  18. 18

    How I make parse function to create var array object from local file?

  19. 19

    returning an object from an android ViewModel to the fragment from a callback function

  20. 20

    How to specify callback function when filtering array with object elements in php?

  21. 21

    Add A Callback To A Prebuilt Asynchronous Function Swift iOS

  22. 22

    How to store the variable of an asynchronous callback function

  23. 23

    DeprecationWarning: Calling an asynchronous function without callback is deprecated

  24. 24

    Iterate over the entries of a Map with an asynchronous callback function

  25. 25

    Meteor: How to catch asynchronous callback function errors

  26. 26

    Retrieving values in an object in an array from local storage

  27. 27

    How to write a callback function for asynchronous calls from helper method to componentDidMount using React JS

  28. 28

    How to pause an asynchronous Swift function until a callback is called when a result is available from another library(JavaScriptCore)

  29. 29

    showing empty array in after pushing data from foreach into it in asynchronous function

HotTag

Archive