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

forEach not calling callback on completion? #2

Closed
apemsel opened this issue Sep 16, 2010 · 4 comments
Closed

forEach not calling callback on completion? #2

apemsel opened this issue Sep 16, 2010 · 4 comments

Comments

@apemsel
Copy link

apemsel commented Sep 16, 2010

Might be a stupid question, but "finished" is never displayed in the following example

var async = require("async");
var sys = require("sys");

var a = ["foo","bar","baz"];

var f = function(arg) {
    sys.log(arg);
}

async.forEach(a, f, function(err) {
    sys.log("finished");
});
@caolan
Copy link
Owner

caolan commented Sep 16, 2010

The iterator function accepts 'arg' and a callback. After completion it must call the callback. Once all iterators have called their callback the forEach callback is executed... I hope that's clear, because the wording is a bit awkward.

Try updating your example to this:

var async = require("async");
var sys = require("sys");

var a = ["foo","bar","baz"];

var f = function(arg, callback) {
    sys.log(arg);
    callback();
}

async.forEach(a, f, function(err) {
    sys.log("finished");
});

That should now display "finished" :)

@caolan
Copy link
Owner

caolan commented Sep 16, 2010

Although this is described in the README, the forEach example doesn't really show it. Perhaps the examples could use a bit of an update.

@apemsel
Copy link
Author

apemsel commented Sep 16, 2010

ahhh thanks, great support ;-)

@apemsel
Copy link
Author

apemsel commented Sep 16, 2010

forgot to close

This issue was closed.
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

2 participants