Why the number of parameters of each task should be the same in async.waterfall

Sorry for the incorrect name of the question, as I do not know how to clearly describe it.

I just met a problem when I use async.waterfall and fix it finally. But I still do not know what is going on behind this.

What I found: When we use async.waterfall, the parameterThe last task should be the same as the next task. Otherwise, he will say "undefined is not a function".

I tried some tests and I am also trying to figure out its source code . Sorry, I'm new to JavaScript. I could not understand a lot of javascript template. This is too hard for me.

After fighting with the source code for more than two hours, I need help. Could you give me some advice on these two issues:

  • how does it handle the error , in what situation will it go to the last callback?

According to my test result, '', null,undefinedit will not lead to a jump. This behavior makes sense to me.

  1. Why should the number of parameters be the same?

If you pass only one argument in the last task, of course, the callback will be undefined. But I would like to know a lot about this.

I can know these words: apply, close, etc., when they are separated in simple demos. But when they come together, I become blind.

Both the short answer and the long answer are much appreciated. If possible, perhaps you can provide some links and I can study it myself.

.

:

var async = require('async');

function test_waterfall() {
  async.waterfall([
    function(callback) {

      /* works - pass all */
      // callback(null, '1')           // null 12
      // callback('','first')          // null 12
      // callback(undefined,'1')       // null 12



      /* skip second, jump to error*/
      // callback('error1')             // error1  undefined
      // callback('erro1', 'value1')    // error1  value1
      // callback({})                   // {}  undefined
      // callback([])                   // []  undefined

      /* wrong -- undefined is not a function*/
      callback()
        // callback('')
        // callback(undefined)
        // callback(null)
    },
    function(arg, callback) {
        // If pass only one argument in last task, 
        //of course, the callback will be undefined.
      callback(null, arg+'2');
    }
  ], function(err, result) {
    console.info(err)
    console.info(result)
  });
}

test_waterfall()
+4
1

, , , api : https://github.com/caolan/async#waterfall

, undefined is not a function, , , :

  • waterfall task/function results/parameters, + extra param as a callback. :

  • , task1 callback(null, 1, 2);, task/function, waterfall, task2(1, 2, callback), , task2 , function(param1, callback){} callback param, 2 callback. , callback, - 2 (null, param) : number is not a function. ( ) task1, task2 callback undefined, , .

arguments . , task1 callback(null, 'a', 'b'), callback arguments[0] (null), argument[1] == 1 argument[2] == 'b'. task2(argument[1], argument[2], callbackFn) . https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments

+1

Source: https://habr.com/ru/post/1618721/


All Articles