I'm going to hazard a guess based on the coding interface and your comments that client.get()
is asynchronous. That means that it calls the callback function passed to it "sometime later", not immediately. Thus, you can't use synchronous coding patterns to collect the results from multiple calls to client.get()
because the results in obj
are not yet available when your function returns. Thus obj
is not yet populated with the results.
If you want to know when multiple asychronous calls are done, then you have to code a very different way. And, the results will ONLY be available inside the callback functions, not at the end of your function.
In all, I see multiple problems with your code:
client.get()
is asynchronous so it hasn't finished yet when your function returns- You should probably be using
i
, notnum
in yourclient.get()
call so that each time through thefor
loop generates a different request. - The value of
i
in the loop has to be frozen in a closure in order to retain it's value for use in the callback function that is called later. - If
obj
was actually undefined, then that may be because you didn't initialize it to an empty object.
Here's one way to do it:
var obj = {};
var remaining = num;
for( var i=1; i<=num; ++i ){
// create a closure here to freeze the value of i in the callback
(function(i) {
client.get(key + ':' + i, function (err, reply) {
obj[i] = reply;
// see if all asynch calls are done yet
--remaining;
if (remaining === 0) {
// all asynch calls to client.get() are done now
// in here, you can use the obj object and the results put on it
}
});
})(i);
}