Search code examples
javascriptangularjsnode.jsasynchronouswaterfall

async waterfall getting Typeerror, nextcallback is not a function


async.waterfall([1,2,3,4].map(function (arrayItem) {
        return function (lastItemResult, nextCallback) {
            // same execution for each item in the array
            var itemResult = (arrayItem+lastItemResult);
            // results carried along from each to the next
            nextCallback(null, itemResult);
        }}), function (err, result) {
        // final callback
    });

so i am new to async and trying a simple example but getting this error, what is wrong with this method TypeError: nextCallback is not a function

what is wrong with the code above?


Solution

  • According to the documentation the signature of a first function should be function (callback) and not function (arg1, callback).

    You can patch your function this way.

    return function (lastItemResult, nextCallback) {
        if (!nextCallback) {
          nextCallback = lastItemResult;
          lastItemResult = 0;
        }
    
        // same execution for each item in the array
        var itemResult = (arrayItem+lastItemResult);
        // results carried along from each to the next
        nextCallback(null, itemResult);
    };