53

I am using the node async lib - https://github.com/caolan/async#forEach and would like to iterate through an object and print out its index key. Once complete I would like execute a callback.

Here is what I have so far but the 'iterating done' is never seen:

    async.forEach(Object.keys(dataObj), function (err, callback){ 
        console.log('*****');

    }, function() {
        console.log('iterating done');
    });  
  1. Why does the final function not get called?

  2. How can I print the object index key?

DLeh
  • 23,806
  • 16
  • 84
  • 128
Ben
  • 6,026
  • 11
  • 51
  • 72

2 Answers2

122

The final function does not get called because async.forEach requires that you call the callback function for every element.

Use something like this:

async.forEach(Object.keys(dataObj), function (item, callback){ 
    console.log(item); // print the key

    // tell async that that particular element of the iterator is done
    callback(); 

}, function(err) {
    console.log('iterating done');
});  
stewe
  • 41,820
  • 13
  • 79
  • 75
  • Thanks @stewe I knew it was something simple! Regards, Ben. – Ben Apr 30 '12 at 20:53
  • 4
    Doesn't the above code call `callback` function for every iteration? Should it be something like `count++; if(dataObj.length == count) callback();` where new variable `var count = 1;` is declared before calling `async.forEach` loop ?? – Ayaz Pasha Mar 03 '14 at 12:13
  • 10
    @AyazPasha async handles that for you. It creates a callback for each element in the array, and doesn't call the final user-supplied callback until it knows the iterator function has processed every element. – Nop Jan 27 '15 at 21:38
  • this is the right idea, but you should be more clear with your language, specifically the comment "//tell async that the iterator has completed". firing the callback doesn't tell async that the iterator is completed; it merely tells async that that particular element of the iterator is done. the final callback will only be invoked once all callbacks get fired. – Alexander Mills Jun 01 '15 at 20:00
0

async.each is very useful and powerful function which is provided by Async Lib .it have 3 fields 1-collection/array 2- iteration 3-callback the collection is referred to the array or collection of objects and iteration is refer to the each iteration and callback is optional . if we are giving callback then it will return the response or say result which you want to show you in the frontend

Applies the function iteratee to each item in coll, in parallel. The iteratee is called with an item from the list, and a callback for when it has finished. If the iteratee passes an error to its callback, the main callback (for the each function) is immediately called with the error.

Note, that since this function applies iteratee to each item in parallel, there is no guarantee that the iteratee functions will complete in order.

exapmle-

 var updateEventCredit = function ( userId, amount ,callback) {
    async.each(userId, function(id, next) {
    var incentiveData = new domain.incentive({
    user_id:userId,
        userName: id.userName,
        amount: id.totalJeeneePrice,
        description: id.description,
    schemeType:id.schemeType
    });

    incentiveData.save(function (err, result) {
        if (err) {
            next(err);
        } else {
                 domain.Events.findOneAndUpdate({
                    user_id: id.ids
                }, {
                    $inc: {
                        eventsCredit: id.totalJeeneePrice
                    }
                },{new:true}, function (err, result) {
                    if (err) {
                        Logger.info("Update status", err)
                        next(err);
                    } else {
                     Logger.info("Update status", result)
                     sendContributionNotification(id.ids,id.totalJeeneePrice);
                     next(null,null);       
                    }
                });
        }
    });
Shashwat Gupta
  • 5,071
  • 41
  • 33