I have a lot of pain with asyncronous call in node.js. I understand that I must enjoy it, but actually I hate it, just because node.js force you to use it, and there is no another option.
I try to run smembers
on different databases. So here I want to be sure that I use all 3 databases therefore I use _.after
. However it doesn't work.
var redisfetcher = function(string, callback1)
{
var retrieved = []
var callback = function(){
callback1(retrieved)
}
var afterAll = _.after(3,callback)
for (var col=0; col<=2; ++col) {
client.select(col, function() {
client.smembers(string, function (err, replies) {
if(!err){
retrieved.push(replies)
}
})
afterAll();
})
}
redisfetcher("offer", function(returnValue) {
console.log(returnValue)
})
I would appreciate if you could help me.
I would appreciate even more if you could show how to run the following code in sync mode.
Use async module.
var async = require('async');
async.mapSeries([0, 1, 2], function (db, done) {
// Here `db` will be `0`, or `1` or `2`, i.e. each element of the array.
// The `done` is an internal callback used by the `async` module
// to track whether a particular element of the array is completed
// to be processed. When you're "done" consuming the `db` element of the
// array, you must call `done` to indicate that `async` can now give you
// yet another element to consume, if there is any left to process.
// When all elements are processed, `async` calls the final function
// passes to it as the last argument. For more information refer to
// very detailed docs of `async` module at
// https://github.com/caolan/async#mapSeries.
async.waterfall([
function (cb) {
client.select(db, cb);
},
function (cb) {
client.smembers(string, cb);
}
], done);
}, function(err, resultArr) {
err && console.trace(err);
// `resultArr` now contains 3 replies.
});
Notice the above code calls mapSeries
which is sequential (don't confuse with synchronous), not parallel as opposed to map
. I suggest using a different Redis connection for each DB in your case, because if called in parallel, such async calls may conflict due to selecting different databases. This may result in running the command against the same database instead of a different as intended. A single connection is ok if you run multiple command against the same database.
For connection pooling, use node-pool.