I am trying to work with async in node js, to control the flow of execution of some functions. In the code below I have three declared functions that should print respectively 'one', 'two' and 'three', along with performing other tasks (the printing is only so that I can see what gets executed when).
async.waterfall([
function(callback) {
settings();
console.log("1");
callback(null, 'one');
},
function(callback) {
profile();
console.log("2");
callback(null, 'two');
},
function(callback) {
console.log("3");
groups();
callback(null, 'two');
}
]);
So I get the error "callback is not a function" in the line of callback(null, 'two'); To be honest, this might as well be a very dummy question, and I do not fully understand how async waterfall works. But I seriously did try, by reading examples, trying to implement short code and trying to play around with it.
Interesting that if async.series is used instead of async.waterfall, there is no such error. However async.series will print 1, 2, 3, three, one, two. The numbers inside the waterfall model are printed in sequence, but the functions inside are not executed in the right order.
It is to be noted that the first two functions - settings and profile - include db reading and calculations whereas the third only prints some results.
To make it simplified,
callback(null, 'one')
means you pass one argument to next. Meaning your function need to accept 1 parameter in front of callback.callback(null, 'one')
follows byfunction(arg1, callback)
wherearg1 = 'one'
callback(null, 'one', 'two')
follows byfunction(arg1, arg2, callback)
wherearg1='one'
andarg2='two'
You also forgot to pass final callback after all the execution.
With
async.waterfall
, results are passed to the next function as arguments. For example:In your case,
callback
on the second function will have received the stringone
as argument. And thus the error you are experiencing.Waterfall
First of all I advice you to use Async auto in place of waterfall model the reason behind this is in waterfall model we will be moving one by one like first we will finish the execution of 1st function then move on to second then 3rd i.e we will go in a series no matters weather our function are dependent on each other or not
Auto
Benefits of auto are that it doesn't really matters weather your functions are dependent on each other as auto can run tasks in series (for dependent functions) and parallel (for the functions which are not dependent on each other)
here in above example we have get_Data function and make_folder function which are not dependent on each other so we can run them parallel and by passing the name of get_Data & make_folder we can restrict out write_file function to run only after both of above function get executed.
for more details you can visit http://caolan.github.io/async/docs.html#auto
even have a look on async.autoInject it have recently been added to async in place of auto. but auto will continue for precious versions of async