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

callback is not a function #1901

Closed
pankaj110987 opened this issue May 27, 2023 · 3 comments
Closed

callback is not a function #1901

pankaj110987 opened this issue May 27, 2023 · 3 comments

Comments

@pankaj110987
Copy link

What version of async are you using?
3.2.4

Which environment did the issue occur in (Node/browser/Babel/Typescript version)
Node Js 18

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

async.eachSeries([1,2,3], async(item, cb) => {
//await delay(6000);
console.log(item);
cb(null);
}, function(done) {
if(done == null){
}
});

What did you expect to happen?

It should work

What was the actual result?

It gives the error cb is not defined.

@jps327
Copy link

jps327 commented Jun 6, 2023

I had a similar problem but then learned that if you're using an async function then the callback is not passed (it is undefined, which is why you get a "callback is not a function" error). Instead, you can just return the value you want to use as the response, or throw an error if there is one, instead of using a callback function and passing in the error and result.

https://caolan.github.io/async/v3/global.html

Wherever we accept a Node-style async function, we also directly accept an ES2017 async function. In this case, the async function will not be passed a final callback argument, and any thrown error will be used as the err argument of the implicit callback, and the return value will be used as the result value.

#1584 (comment)

We treat async functions differently -- we do not pass them a callback. Instead, simply return a value (or throw an error).

@abhaysinghs772
Copy link

abhaysinghs772 commented Aug 5, 2023

@pankaj110987, i had also the same issue this is how you can resolve this issue

await new Promise((resolve, reject) => {
             async.mapSeries(
                [1, 2, 3, 4],
                async (id) => {
                    let user;
                    try {
                        user = await findOneById(1);
                        if (user) {
                            resolve(user);
                        }
                    } catch (error) {
                        reject(error);
                    }
                }
            )
        });

@aearly
Copy link
Collaborator

aearly commented Aug 14, 2023

@abhaysinghs772 that wont work like you expect, it'll resolve after the first iteration. @jps327 's comment is correct.

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

No branches or pull requests

4 participants