Supun Wimukthi
Supun Wimukthi

Reputation: 85

Error handling in async.parallel

I have a function which uses async.parallel function to call two functions. But I don't know how to handle errors when async.parallel is used. If one of the two functions throw errors, how do I handle them?

exports.getProductconfigWithProductList = function(req, res){

var apiVersion = req.param('version', null);
var product_id = req.params.productId;
dbDriver = determineDriver('es');

async.parallel([
        function(callback) {
            dbDriver.getProductconfig(product_id,function(data){
                callback(null, data);
            });
        },
        function(callback) {
            var productListPromise = getProductListData();
            productListPromise.then(function(data) {
                callback(null, data);
            });
        }
    ],
    function(err, results) {
        if(!err){
            var data = results[0];
            data[1] = results[1];
            res.send(data);
            res.end();
        } else {
            console.log('<<<<<<<<<<<<<<'+err+'>>>>>>>>>>>>>>>');
            res.send(err);
            res.end();
        }

    }
);
};

Upvotes: 0

Views: 3399

Answers (1)

rsp
rsp

Reputation: 111336

When you have:

async.parallel([
    func1,
    func2,
], (err, data) => {
    if (err) {
        // you handle the errors here
    }
});

It's explained in more detail in the docs:

and in the issue on GitHub here:

Upvotes: 2

Related Questions