I need to do several request to Redis db, and I need the response from a request to do the next one.
Let's analyze this example: (I'm using node_redis for the requests)
var message-id = undefined;
// request the next id
client.incr("message-id", function(err, id) {
message-id = id;
});
// use the 'next id' for storing a message
client.hmset("messages", message-id, messageContent);
This way doesn't work, because message-id id still undefined when I call client.hmdet().
The following is the lazy way, and it consist in put the client.hmdet() inside the client.incr() callback.
var message-id = undefined;
// request the next id
client.incr("message-id", function(err, id) {
message-id = id;
// use the 'next id' for storing a message
client.hmset("messages", message-id, messageContent, function(){
//more request here
});
});
I think this is not a good way to proceed, because if I have lots chained request, I need to put each request inside its previous request callback, and this will create a block of code hard to maintain.
I'm interested in the best practices for doing lots of request asynchronously. Can someone advise me?