0

I have 2 nested async.each. They look like:

 async.each(result, function(row, callbackrow) {
   tot = 0;
   console.log('UID:', row.uid);

   async.each(row.vbs, function(vb, callback){
     checkInteractions(row.uid, vb.vbNID, function(data){
       console.log(data);
       callback();
     });
   }, function(err){ console.log('done 1'); });

   callbackrow();  
}, function(err){ console.log("done all"); });

My problem is, the checkinteraction async call causes problems. Without it, console logs are called from second each for each one of the first async each. With it, I get the done all message, and after the nested (second) async each executes its code. I need it to be like a synchronus call of a for inside a for. For each element of first for, the second one to be executed before stepping to next index in the first for.

1 Answer 1

1

I think callbackrow should be passed as final callback of the inner loop, so that the outer loop knows when to step to the next iteration, like so:

async.each(result, function(row, callbackrow) {
  tot = 0;
  console.log('UID:', row.uid);

  async.each(row.vbs, function(vb, callback){
    checkInteractions(row.uid, vb.vbNID, function(data){
      console.log(data);
      callback();
    });
  }, function(err){ 
    console.log('done 1'); 
    callbackrow();
  });

}, function(err){ console.log("done all"); });
Sign up to request clarification or add additional context in comments.

1 Comment

tryed it too. i get the final callbacks console logs in order. that data log from async check it's still executed after the outer loop call its final log callback.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.