I have been using async module to manage my code flow and it's indeed a very powerful lib. However, as the code grows bigger and logical flow become more complex, I run into a problem with too many async_callback to manage. Some pseudo code to demo the issue as below
async.auto({
getjobs:function(cb){},
loopjobs:['getjobs']{
async.map(jobs,dojob(job, cb_map),cb);
},
}, function(err, result) {
//handle
});
function dojob(job, callback) {
async.auto({
step1:function(cb){},
step2:['step1', function(cb) {}],
step3:['step1', function(cb) {
moreloops(params, cb);
}]
}, callback)
}
function moreloops(params, cb) {
async.map(params, doloop(param, cb_map), cb);
}
function dbloop(param, cb) {
//dosomething
cb();
}
In this flow, a combination of loops
, sequential
, parallel
and conditional
async callback is being used due to the need of the business logic. It become quite difficult to troubleshoot if any function is not returning a callback for any reason. I tried to debug using logging framework but obviously it's not very efficient due to the asynchronous nature of node.js.
Is there any good way to
- Improve the flow in coding
- Identify in which function the flow stops.
Thanks!