Async: Using an AsyncFunction as the iteratee function results in iteratee being passed as undefined

Created on 26 Sep 2017  ·  4Comments  ·  Source: caolan/async

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.

docs question

Most helpful comment

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

All 4 comments

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

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

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

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

Was this page helpful?
0 / 5 - 0 ratings