Thursday, 27 April 2017

Transform async.parallel to async.auto

I have an async.parallel with 2 functions, and now I need to transform to async.auto or async.waterfall because I need to pass a value from the first function to the second.

Both functions return an array. In the second function the param timeAdjust will has the value from array1.timeAdjust

Can anyone help me?

The code:

router.get('/:machineID', function(req, res, next) {
    var machineID = req.params.machineID;

    async.parallel({
        one: function(callback){
            auxPrinters.function1(machineID, 'ent', function(array1){
                callback(null, array1);
            });
        },
        two: function(callback){
            auxPrinters.function2(machineID, 'ent', timeAdjust, function(array2){
                callback(null, array2);
            });
        },      
    },
    function(err, results){  //results.one=array1 & results.two=array2
        if(err) {console.log("****** ERROR:"); console.log(err);}
        else{
            console.dir(results);
            res.render('jobs', {title: 'Job Info', results});
        }
    });
});



via Sergi Nadal

No comments:

Post a Comment