Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

213
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda