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

215
Visualizações
Using async waterfall to pass arguments

Consider a case wherein the callback value of the first function in waterfall needs to be used multiple times in subsequent functions to finally coalesce the results. In the following code, baseFn should return an array in the callback, which should then be processed element by element as indicated in subFn1 and subFn2. Thereafter, the return callbacks of subFn1 and subFn2 must be appended into a new array to produce the final result. I have trying to see, if there is a way to achieve this using async.series and async.apply, but haven't able to get much further. Please advise.

async.waterfall ([  

  function baseFn(baseCb) {      
    //a method that gets an array of elements  
    return (null, elementArray);
  },  
  function subFn1(elemArr, subFnCb) {  
    var elem = elemArr[0];  
    //a method that returns processed element  
    return (null, proc_elem1);  
  },  
  function subFn2(elemArr,subFnCb) {  
    var elem = elemArr[1];
    //a method that returns processed element  
    return (null, proc_elem2);  
  },  
  function aggOut() {}  
], function getOutput (err, out) {  
      console.log(out);  
})
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Since subFn1() and subFn2() both depend on baseFn, it's best to use async.auto() because we can specify the dependency of subFn1 and subFn2 is baseFn(). Here is the code:

var async = require('async');

async.auto({
  baseFn: function(autoCb) {
    var elementArray = ['one', 'two'];
    autoCb(null, elementArray);
  },
  subFn1: ['baseFn', function(results, autoCb) {
    var elem = results.baseFn[0];
    console.log('subFn1', elem);

    var proc_elem1 = 'proc_elem1';
    autoCb(null, proc_elem1);
  }],
  subFn2: ['baseFn', function(results, autoCb) {
    var elem = results.baseFn[1];
    console.log('subFn2', elem);

    var proc_elem2 = 'proc_elem2';
    autoCb(null, proc_elem2);
  }]
}, function(err, results) {
  console.log('results', results);
});

The output will look something like this (note that the order of subFn1 and subFn2 completion is not guaranteed since they run in parallel. However, the final 'results' object in the final callback will have all results):

subFn1 one
subFn2 two
results { baseFn: [ 'one', 'two' ],
  subFn1: 'proc_elem1',
  subFn2: 'proc_elem2' }
over 4 years ago · Santiago Trujillo Relatório

0

const baseFn = function(baseCb) {
  // ...
  return baseCb(elementArray);
}

baseFn(function(elemArr) {
  // since subFn1 and subFn2 both need elemArr independently,
  // lets use async.parallel here.
  async.parallel([
    function subFn1(elemArr, subFnCb) {
      // ...
      return subFnCb(null, proc_elem1);
    }),
    function subFn2(elemArr, subFnCb) {
      // ...
      return subFnCb(null, proc_elem2);
    }),
  ], function(err, result) {
    // async.parallel callback with proc_elem1 and 2 in result.
    // you can now create your final result with elementArray
    // and both proc_elem.
    console.log([elemArr, result[0], result[1]]);
  });
});
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