Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
sjungwirth opened this issue Oct 5, 2017 · 13 comments
Closed

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

sjungwirth opened this issue Oct 5, 2017 · 13 comments

Comments

@sjungwirth
Copy link

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
@aearly
Copy link
Collaborator

aearly commented Oct 5, 2017

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

@sjungwirth
Copy link
Author

sjungwirth commented Oct 5, 2017

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?

@aearly
Copy link
Collaborator

aearly commented Oct 5, 2017

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) {
    //...
  }

@chak774
Copy link

chak774 commented Jan 4, 2019

Then how to return error? Just using throw?

@josefanostylus
Copy link

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

@pravynandas
Copy link

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) {
    //...
  }

@aearly
Copy link
Collaborator

aearly commented Apr 9, 2020

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

@pravynandas
Copy link

Thanks.

@kindacoder
Copy link

kindacoder commented Jun 7, 2022

IIFE can be helpful here.
Here is how i used it.

function doSomethingAsynchronously(_callback) {
    ;(async () => {
        await callAnAsyncFunction();
    })()
    _callback(null)
  }

@aearly
Copy link
Collaborator

aearly commented Jun 7, 2022

IIFE can be helpful here. Here is how i used it.

function doSomethingAsynchronously(_callback) {
    ;(async () => {
        await callAnAsyncFunction();
    })()
    _callback(null)
  }

This wont do what you expect. The _callback will be called before the async function returns. Moving the callback inside the async IIFE will work, though.

@kindacoder
Copy link

kindacoder commented Jun 7, 2022

a

@theshubhamjoshi
Copy link

b

@haayhappen
Copy link

c

Repository owner locked as spam and limited conversation to collaborators Nov 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

8 participants