0

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

  1. Improve the flow in coding
  2. Identify in which function the flow stops.

Thanks!

Lee
  • 2,874
  • 3
  • 27
  • 51

1 Answers1

0

Few ways to improve debugging

  1. Use vasync module It's like async but with better debugging

  2. vasync may not have all the tools you need so promisify (bluebird) all callbacks and use promises so that functions will return at least .then and .catch

For example, in request module:

var request = require('request');
function makeRequest(cb) {
    request(url, function(err, res, body) {
        cb(err, body);
    });
}

becomes

var Promise = require('bluebird');
var request = Promise.promisify(require("request"));
Promise.promisifyAll(request);

function makeRequest(cb) {
    request(url)
        .then(function(response) {
            var body = response.body;
            cb(null, body);
        })
        .catch(cb);
}

Just pass all the success in .then and errors in .catch and pass errors back to the the highest level.

Taku
  • 5,639
  • 2
  • 42
  • 31