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

Using an AsyncFunction as the iteratee function results in iteratee being passed as undefined #1475

Closed
JustinBeaudry opened this issue Sep 26, 2017 · 4 comments

Comments

@JustinBeaudry
Copy link

JustinBeaudry commented Sep 26, 2017

What version of async are you using?
2.5.0

Which environment did the issue occur in (Node version/browser version)
OS Version: OS X 10.12.6
Node Version: 8.4.0
Browser Version: N/A

What did you do?

// templateNames is an array of strings
async.map(templateNames, async (templateName, next) => {   
  let template;
  try {
    template = await utils.getFile(templateName);
  } catch(err) {
    next(err);
  }
...
// and so on

What did you expect to happen?
next is an iteratee function

What was the actual result?
next is undefined


I ended up just using the .then().catch() syntax to accomplish my goal but still wanted to share that using an AsyncFunction (NOTE: this is not the async definition but the native AsyncFunction Object) results in the iteratee (next) being of type undefined.

@aearly
Copy link
Collaborator

aearly commented Sep 26, 2017

async functions are treated differently as of 2.3.0 -- they are not passed a callback. Instead, just return a value (or Promise). In fact, in your example, even the try/catch is not necessary because any errors thrown will be caught by Async. This should simplify your code a bit.

// templateNames is an array of strings
async.map(templateNames, async templateName => {   
  const template = await utils.getFile(templateName);
...
// and so on

AsyncFunction docs: http://caolan.github.io/async/global.html

@JustinBeaudry
Copy link
Author

🤦‍♂️ Reading that portion of the docs more clearly would have been smart. Thanks.

@aearly
Copy link
Collaborator

aearly commented Sep 26, 2017

In fairness, that description is not that obvious on the docs site... 🙄 📜

@JustinBeaudry
Copy link
Author

JustinBeaudry commented Sep 26, 2017

I actually think it's pretty clear. I glanced over the docs initially and missed that portion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants