Here is the simplified corrected version of it
function secondLevel(value2, val, callback) { // ... User.addCampaignResponse(email, campId, tempObj, options, function (err, results) { if (err) { throw err; } Campaign.updateResponse(_id, function (err, results2) { // throw the error to the highest level if there is one if (!err) throw err; // Finish the eachOfSeries if all is ok callback(); }); }); }
function firstLevel(value1, i, callback) { // ... mailjet .get("messagesentstatistics") .request({ // ... }) .then((result) => { // ... async.eachOfSeries(data, secondLevel, function (err) { // throw the error to the highest level if there is one if (!err) throw err; // Finish the eachOfSeries if all is ok callback(); }); }) .catch((err) => { // throw the error to the highest level throw err; }); }
// // Start of our program // async.eachOfSeries(res, function (value, camp, callback) { // ... async.eachOfSeries(arr, firstLevel, function (err) { // throw the error to the highest level if there is one if (!err) throw err; // Finish the eachOfSeries if all is ok callback(); }); }, function (err) { if (err) { // Here we re done with an error // ... return; } // We did it, no, error // ... });