Async: forEachOf - final callback not being called

Created on 29 Jun 2015  ·  1Comment  ·  Source: caolan/async

EDIT: Issue resolved (I spoke too soon!)... (look below)

I'm using the .forEachOf method to throw a callback after iterating through a given object. The only problem is, the final callback passed as the third argument to forEachOf does not ever seem to be thrown.

Here is some sample code of the problem:

var async = require('async');

var anArray = [];

var testObj = {
    'a': '1',
    'b': '2',
    'c': '3'
};

async.forEachOf(testObj, function (elem, key, callback) {
    console.log(elem);

    anArray.push(elem);
}, function (err) {
    if (err) {
        console.error(err.message);
    }

    console.log("This line should be printed at the end.", anArray);
});

Here it is on Runnable: http://code.runnable.com/VZELB92Qa1Y7JQuq/async-foreachof-node-js-not-working-2-for-callback

Now I could be doing something totally wrong here, but I've posted this question to /r/node on reddit and Stack Overflow, and have gotten no helpful responses. What am I doing wrong here? This is driving me insane!!!

EDIT: It seems as though in this initial example, I forgot to add a 'return callback()' statement. In the particular example where I actually ran into this problem in my code, I have a second asynchronous method in which I make this 'return callback()' statement. It seems as though I'll need to use another control flow feature to call the callback once the second asynchronous method has finished running. My apologies. Please close this issue ASAP!!!

On the other hand, I believe that it will be helpful to other future users using this method to know how to properly use the 'return callback()' statement. The documentation is definitely lacking in this area, and there isn't very much out there on individual methods in this library as-is so finding help is incredibly slow, tedious, and difficult!

question

Most helpful comment

dude you need to call 'callback()' after every iteration.

async.forEachOf(testObj, function (elem, key, callback) {
    console.log(elem);
    anArray.push(elem);
callback() //<<<<<<<<<
},...

>All comments

dude you need to call 'callback()' after every iteration.

async.forEachOf(testObj, function (elem, key, callback) {
    console.log(elem);
    anArray.push(elem);
callback() //<<<<<<<<<
},...
Was this page helpful?
0 / 5 - 0 ratings