Thursday, 27 April 2017

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);  
})



via skarred14

No comments:

Post a Comment