What is the difference between async.waterfall and async.series
Asked Answered
I

4

119

The nodejs async module: https://github.com/caolan/async provides 2 similar methods, async.waterfall and async.series.

What is the difference between them?

Irreversible answered 13/2, 2012 at 10:3 Comment(0)
K
174

It appears that async.waterfall allows each function to pass its results on to the next function, while async.series passes all results to the final callback. At a higher level, async.waterfall would be for a data pipeline ("given 2, multiply it by 3, add 2, and divide by 17"), while async.series would be for discrete tasks that must be performed in order, but are otherwise separate.

Karachi answered 13/2, 2012 at 10:13 Comment(3)
Is it possible for either of these two functions to return a value? I've read that it's possible, but I can't find relevant information anywhere in the documentation.Cornucopia
@AndersonGreen: Nope. Looking at the library source, neither waterfall nor series returns a value. It's expected that whatever the result is will be used in the optional callback parameter.Karachi
The above answer may be correct back to 2012, but the correct one is the same as the next, which is: series() as it's named that ALL results as a series passed to the Final callback, and waterfall is the LAST result passed to the Final callback. See Mozilla DeveloperFrequentation
M
53

Both functions pass the return value, of every function to the next, then when done will call the main callback, passing its error, if an error happens.

The difference is that async.series(), once the series have finished, will pass all the results to the main callback. async.waterfall() will pass to the main callback only the result of the last function called.

Mudfish answered 8/1, 2013 at 14:14 Comment(3)
This should be the answer ;-)Yalonda
@Mario "Both functions pass the callback of the previous function" OR "Both functions pass the result of the previous function" ?Duration
@user1451111, Result, lol... I am sure I had written that the first time though...Mudfish
D
27

async.waterfall() is dealing with an action that relies on the previous outcome.

async.series() is dealing with an action that wants to see all the result at the end

Deon answered 31/8, 2016 at 13:30 Comment(2)
Awesomely created images. Did you create them yourself or got them somewhere else?Duration
you should put the pictures in the answer, people will probably miss them o/wManiacal
M
2

I consider async.waterfall to be harmful, because it's hard to refactor once written and also error-prone since if you supply more arguments, other functions much change the signature.

I highly recommend async.autoInject as a great alternative, to async.waterfall. https://caolan.github.io/async/autoInject.js.html

If you do choose to use async.waterfall, I recommend storing everything in one object, so your functions don't have to change length/signatures, like so:

warning: this is a bad pattern

async.waterfall([
  cb => {
    cb(null, "one", "two");
  },
  (one, two, cb) => {
    cb(null, 1, 2, 3, 4);
  },
  (one,two,three,four,cb) => {
     // ...
  }
])

don't do it the above way. This is a much better pattern to use:

async.waterfall([
  cb => {
    cb(null, {one:"one", two:"two"});
  },
  (v, cb) => {
    cb(null, [1, 2, 3, 4]);
  },
  (v,cb) => {
     // ...
  }
])

that way you won't pull your hair out trying to make sure the function arguments have the right length. The first function only accepts one arg - callback. All the remaining ones should accept two arguments - a value and callback. Stick to the pattern and you will remain sane!

Maniacal answered 4/2, 2019 at 2:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.