question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Callback is undefined on async/await function

See original GitHub issue

Not sure this is a bug or not. It worked on my production environment right now (docker, node:7.7.0-alpine, async@2.1.5). Please guide me.

What version of async are you using? 2.3.0

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

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

const { eachSeries } = require('async')

eachSeries( 
  [1,2,3], 
  async (item, cb) => {   // <----- it works if remove ```async```
    console.log( 'item:', item )
    console.log( 'cb:', typeof(cb) )
    console.log()
    cb()
  }, 
  err => {
    console.log('err:', err)
  }
)

What did you expect to happen?

item: 1
cb: function

item: 2
cb: function

item: 3
cb: function

err: null

What was the actual result?

item: 1
cb: undefined

err: TypeError: cb is not a function

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:7

github_iconTop GitHub Comments

8reactions
wootsaejaocommented, Apr 18, 2017

Figured out that I have to use it this way:

const { eachSeries } = require('async')

eachSeries( 
  [1,2,3], 
  async item => {
    console.log( 'item:', item )
    console.log()
    Promise.resolve() // <-- instead of callback
  }, 
  err => {
    console.log('err:', err)
  }
)

Result:

item: 1

item: 2

item: 3

err: null
5reactions
aearlycommented, Apr 18, 2017

You are correct – the cb is no longer passed to async functions. (Changed in 2.3.0)

That final Promise.resolve() is not needed. For async functions, the return value becomes the result.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JS async function returning undefined although calling it ...
query is taking a callback function, which implies it isn't returning a promise. You need to either: Find out how to make the...
Read more >
AWS Lambda function handler in Node.js
The callback function takes two arguments: an Error and a response. When you call it, Lambda waits for the event loop to be...
Read more >
JavaScript Promises and Async/Await: As Fast As Possible™
If no payload passed to these function, the payload is undefined. When resolve is called, then method (AKA handler) is called which executes...
Read more >
How to rewrite a callback function in Promise form and async ...
It would be so great to be able to develop using modern JavaScript and start making use of the async/await functionality... If you...
Read more >
async function - JavaScript - MDN Web Docs - Mozilla
The behavior of async / await is similar to combining generators and promises. Async functions always return a promise. If the return value...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found