The above code may not do what you expect. You run each .get() in sequence, but they cannot call back sequentially, so the results can come out in any order. If you want to pass results, rather than collect them in memory, you need .get() in sequence.
I think the caolans asynchronous library makes this a lot easier. Heres one way you could use it to get each element in a sequence (warning, untested):
app.get("/facility", function(req, res) { rc.keys("FACILITY*", function(err, replies) { var i = 0; res.write("["); async.forEachSeries(replies, function(reply, callback){ rc.get(reply, function(err, reply) { if (err){ callback(err); return; } res.write(reply); if (i < replies.length) { res.write(","); } i++; callback(); }); }, function(err){ if (err) {
If you don't care about ordering, just use async.forEach() .
If you don't mind collecting results and want them to come back sequentially, you could use async.map() like this (warning, also not verified):
app.get("/facility", function(req, res) { rc.keys("FACILITY*", function(err, replies) { async.map(replies, rc.get.bind(rc), function(err, replies){ if (err) {
source share