async-waterfall icon indicating copy to clipboard operation
async-waterfall copied to clipboard

Example when using iterator and exit function

Open loretoparisi opened this issue 6 years ago • 1 comments

Running this snippet

var step=function(item,nextCallback) {
 console.log("next",item)
 nextCallback.apply(this,[item,nextCallback])
}
waterfall([...new Array(5).keys()].map(function (arrayItem) {
  return (nextCallback) => {
	step(arrayItem,nextCallback)    
  }
}));

I get only

next 0
next 1

or adding an exit function

waterfall([...new Array(5).keys()].map(function (arrayItem) {
  return (nextCallback) => {
	step(arrayItem,nextCallback)    
  }
}),(results)=> {
   console.log(results)
});

I get 1 as result that is correct, but it ends after two iterations.

What's wrong?

loretoparisi avatar Oct 26 '17 15:10 loretoparisi

I'm not a contributor, but I found this issue and tried to understand what your snippet is meant to do. I honestly cannot tell.

I think you might mean to write something like this:

waterfall([...new Array(5).keys()].map(function (arrayItem) {
  return (nextCallback) => {
	console.log(arrayItem);
        nextCallback();    
  }
}));

Which works perfectly fine (and prints 0, 1, 2, 3, 4 consecutively). I don't know what your step function is meant to do, but I believe it's causing some kind of early termination due to some intermediate function in the chain not taking a callback or not calling its callback. I'm not really sure, as it's quite confusingly written.

I really hope this helps. If not, please elucidate to me your original intentions.

dshepsis avatar Mar 23 '18 20:03 dshepsis