Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

86
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda