async. series invokes your functions serially (waiting for each preceding one to finish before starting next). async. parallel will launch them all simultaneously (or whatever passes for simultaneous in one-thread land, anyway).
If you use the async keyword before a function definition, you can then use await within the function. When you await a promise, the function is paused in a non-blocking way until the promise settles. If the promise fulfills, you get the value back.
eachSeries(coll, iteratee, callbackopt) import eachSeries from 'async/eachSeries'; The same as each but runs only a single async operation at a time. Note, that unlike each , this function applies iteratee to each item in series and therefore the iteratee functions will complete in order.
A queue object based on an asynchronous function can be created which is passed as a worker. Task: Here, it takes two parameters, first — the task to be performed and second — the callback function.
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.
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.
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
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With