async.waterfall breaks when given an async function (node 8)

Created on 5 Oct 2017  ·  8Comments  ·  Source: caolan/async

What version of async are you using?
2.5.0

Which environment did the issue occur in (Node version/browser version)
Node 8

What did you do? Please include a minimal reproducable case illustrating issue.

async=require('async')

async function myFirstFunction(callback) {
     callback(null, 'one', 'two');
}

function mySecondFunction(arg1, arg2, callback) {
     // arg1 now equals 'one' and arg2 now equals 'two'
     callback(null, 'three');
}

async function myLastFunction(arg1, callback) {
     // arg1 now equals 'three'
     callback(null, 'done');
}

async.waterfall([
     myFirstFunction,
     mySecondFunction,
     myLastFunction,
], function (err, result) {
     // result now equals 'done'
     console.log(err, result)
});

What did you expect to happen?
to not have an exception

What was the actual result?

> TypeError: callback is not a function
    at myFirstFunction (repl:2:5)
    at /Users/scott/node_modules/async/dist/async.js:143:27
    at /Users/scott/node_modules/async/dist/async.js:21:12
    at nextTask (/Users/scott/node_modules/async/dist/async.js:5297:14)
    at Object.waterfall (/Users/scott/node_modules/async/dist/async.js:5307:5)
    at repl:1:7
    at ContextifyScript.Script.runInThisContext (vm.js:44:33)
    at REPLServer.defaultEval (repl.js:239:29)
    at bound (domain.js:301:14)
    at REPLServer.runBound [as eval] (domain.js:314:12) undefined
docs question

Most helpful comment

Yeah, you could do something like:

async.waterfall([
  // ...
  async function (arg1, arg2) {
    //...
    const arg3 = await foo()
    return [arg1, arg2, arg3]
  },
  function ([arg1, arg2, arg3], callback) {
    //...
  }

All 8 comments

Callbacks are not passed to async functions, instead, simply return a value.

In the case of the first function above, where more than one argument is passed to callback in myFirstFunction, should we instead return an array?

Yeah, you could do something like:

async.waterfall([
  // ...
  async function (arg1, arg2) {
    //...
    const arg3 = await foo()
    return [arg1, arg2, arg3]
  },
  function ([arg1, arg2, arg3], callback) {
    //...
  }

Then how to return error? Just using throw?

Any answers to the above question? I believe to bail when encountered an error in a async function , you still need to call the "next" callback

How about this?

async.waterfall([
  // ...
  async function (arg1, arg2, callback) {
    //...
    try {
      const arg3 = await foo()
      return [arg1, arg2, arg3]
    } catch (err) {
      callback('An error occured:' + err.message);
    }
  },
  function ([arg1, arg2, arg3], callback) {
    //...
  }

async functions don't get passed callbacks. Just throw an error.

Thanks.

Was this page helpful?
0 / 5 - 0 ratings