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

applyEach() does not work as expected when (optional) callback is omitted? #1278

Closed
charleswhchan opened this issue Sep 5, 2016 · 2 comments
Labels

Comments

@charleswhchan
Copy link

charleswhchan commented Sep 5, 2016

async.js: 2.0.1
node.js: 0.10.46

Sample code:

var async = require("async");

function hello(arg) {
    console.log("arg value: " + arg);
}

async.applyEach( [ hello, hello ], 'test arg');

Expected result:

arg value: test arg
arg value: test arg

Actual result:

arg value: function (err, v) {
                results[index] = v;
                callback(err);
            }
arg value: function (err, v) {
                results[index] = v;
                callback(err);
            }

I read the doc for applyEach() a number of times and I don't know if I am doing something wrong. If I add a callback, then I get the expected result:

var async = require("async");

function hello(arg) {
    console.log("arg value: " + arg);
}

async.applyEach( [ hello, hello ], 'test arg', function (err, callback) {
    // noop
});

Result:

arg value: test arg
arg value: test arg
@hargasinski
Copy link
Collaborator

Thanks for the report! The documentation for appyEach needs to be clarified a little. It's signature is (fns, ...args, callback). You can only omit callback if you don't pass anything for args. For example, you only called it with async.applyEach([ hello, hello]);. In this case, it will return a function with the signature (..args, callback).

In both cases, when calling the function returned by async.applyEach or calling async.applyEach with more than one argument, it will treat the final argument passed as callback. So, in the async.applyEach([ hello, hello ], 'test arg') case, test arg is being treated as callback, and args is empty.

The reason you are seeing a function being logged is because appyEach expects the functions passed in fns to be asynchronous, so they should have a callback as their last parameter. You'll notice that because of this, the callback you passed in your second example is never actually called because it's waiting for two hello functions to call the callback passed to it by appyEach, so they should actually be:

function hello(arg, callback) {
    console.log("arg value: " + arg);
    callback();
};

Hope that clarifies things, I'll try to improve the documentation a bit.

@charleswhchan
Copy link
Author

Thanks @hargasinski

hargasinski added a commit to hargasinski/async that referenced this issue Oct 7, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants