I am trying to reduce the nesting of async calls (node + socket.io) by using async.waterfall
and I ended up having to append parameters down the waterfall because they are needed later. This code might explain better:
// Original version:
socket event: turn action
socket.on('turn action', function(gameId, turnAction, clientFn) {
socket.get('corp', function(err, corp) {
gameProvider.processTurnAction(gameId, corp.id, turnAction, function(err, msg, game) {
clientFn(msg, game);
});
});
});
// async.js version
async.waterfall([
function(callback) {
socket.on('turn action', function(gameId, turnAction, clientFn) {
callback(null, gameId, turnAction, clientFn);
});
},
function(gameId, turnAction, clientFn, callback) {
socket.get('corp', function(err, corp) {
callback(null, gameId, turnAction, clientFn, corp);
});
},
function(gameId, turnAction, clientFn, corp, callback) {
gameProvider.processTurnAction(gameId, corp.id, turnAction, function(err, msg, game) {
clientFn(msg,game);
});
}
]);
The goal was readability but I find the redundant param passing adds clutter. I know that I can declare variables before the call to async.waterfall and store the params as needed for later use in the chain but that doesn't help with readability.
Is there a way to make this more elegant?