'Async.eachOfSeries not looping

I tried aysnc.eachOfSeries but the code does not loop. It stops executing in the first iteration itself and freezes. I guess I made some error in returning the callback.

I also tried putting a callback inside the else block but gives callback already called error.

This async.eachOfSeries is nested inside one more eachOfSeries.

async.eachOfSeries(data, function (value2, val, callback) {
    let jsonObj = data[val];
    let email = jsonObj.ToEmail;
    jsonObj['retailer'] = res[camp].retailer;
    jsonObj['summary'] = 'f';
    let tempObj = {};
    tempObj[id] = jsonObj;
    let options = { new: true};
    let campId = id;
    User.addCampaignResponse(email, campId, tempObj, options, function (err, results) {
        if (err) {
                 throw err;
        } else {
            console.log("gets printed once");
            Campaign.updateResponse(_id, function (err, results2) {
                if (err)
                    throw err;
                else {
                    console.log("Gets printed once");
                    callback(); //tried this but gives callback already called error
                    }
                }) // console.log(results);
            }
        })
    }, function (err) {
        console.log("Doesn't print");
            callback();
  })


Solution 1:[1]

You must use it like this:

//async.eachOfSeries(data, function (value2, val, callback) { => wrong
async.eachOfSeries(data, function (value2, callback) {
    //
})

Solution 2:[2]

Thought I'd leave this here in case someone else hits this thread. The problem is that on your optional callback at the end:

    }, function (err) {
        console.log("Doesn't print");
        callback();
   });

You're calling the iterator callback and not an outer callback. In the context of your code you probably have your async inside other function which callback is the one you should call on your async's optional callback at the end. Or just don't use the optional function, but never let it go through your iterator callback after ending the loop.

You should also test for error, something like:

}, function(err){
  if(err){
    debug('Something went wrong in async: ' +err);
    outerCallback(err, somethingIfitsthecase);
  }else{ 
    outerCallback(null, somethingIfitsthecase);
  }
});

Check caolan's documentation as Parth Acharya recommended. Thanks Best regards Pedro Velez Silva

Solution 3:[3]

It is supposed to work that way.

if an error occurs, which is happening in your case. It runs the final callback function that you've provided.

Link :- https://caolan.github.io/async/docs.html#eachOfSeries

A callback which is called when all iteratee functions have finished, or an error occurs. Invoked with (err).

Solution 4:[4]

I had a similar dilemma and I solved it by adding a callback in the async param and calling it at the end of the method as shown below.

async.eachOfSeries(data, async (item, index, callback: any) => {
    // method body 
    callback();
})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ozgur
Solution 2 FilhoDumMocho
Solution 3 Parth Acharya
Solution 4 Yonatan Dawit