Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

83
Views
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});
        }
    });
});
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

here is your answer, but before that it is weird for me that you want to write array1.timeAdjust if array is an array, so I will just believe that array1 is an object

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

    async.waterfall([
        function(callback){
            auxPrinters.function1(machineID, 'ent', function(array1){
                callback(null, array1);
            });
        },
        function(array1, callback){
            var result = {}
            result.one = array1
            timeAdjust = array1.timeAdjust
            auxPrinters.function2(machineID, 'ent', timeAdjust, function(array2){
                result.two = array1
                callback(null, result);
            });
        },      
    ],
    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});
        }
    });
});

as you can see I didn't pass array.timeAdjust but the full array1 since you want array1 and array2 at the final callback

over 4 years ago · Santiago Trujillo Report

0

Final Solution:

async.waterfall([
        function(callback){
            console.log("****** ASYNC JOBS 1");
            auxPrinters.function1(machineID, 'ent', function(array1){
                callback(null, array1);
            });
        },
        function(array1, callback){
            console.log("****** ASYNC JOBS 2");
            auxPrinters.function2(machineID, 'ent', array1, function(array2){
                callback(null, array1, array2);
            });
        },      
    ],
    function(err, array1, array2){
        if(err) {console.log("****** ERROR ASYNC:"); console.log(err);}
        else{
            res.render('jobs', {title: 'Job Info', array1, array2});
        }
    });
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!