I am using redis and node (with node_redis) and I would like to serialize data from redis into an XML file (using simple-xml-writer), but I stumble upon the asynchronous behavior of node.
I have datasets for a,b,c,d and e stored as a hash in redis, the keys are data:a, data:b data:c … and each key accesses a hash. Now my XML file should look like this:
<root>
<data record="a">
(data for a)
</data>
<data record="b">
(data for b)
</data>
...
</root>
My approach is to do something like:
myobjects = Array.new();
["a","b","c","d","e"].forEach(function(str) {
database.hmget("data:" + str,function(err,obj){ myobjects.push(obj) });
});
now_serialize_myobjects();
Is it possible to wait for the forEach-loop to finish and be sure all objects in database.hmget() are stored? So when calling the function now_serialize_myobjects(), all objects covered?
What is the best approach to that problem?
Simplest approach
But why not serialize as requests return?
if you are loading a ton of stuff second one is better I think