caolan / async

Async utilities for node and the browser
http://caolan.github.io/async/
MIT License
28.15k stars 2.41k forks source link

callback is not a function #1901

Closed pankaj110987 closed 1 year ago

pankaj110987 commented 1 year ago

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 commented 1 year ago

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.

https://github.com/caolan/async/issues/1584#issuecomment-424434768

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

abhaysinghs772 commented 1 year ago

@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 commented 1 year ago

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