javascript-sync-async-foreach icon indicating copy to clipboard operation
javascript-sync-async-foreach copied to clipboard

control does not wait for asynchrounous calls in foreach to complete

Open simran66 opened this issue 7 years ago • 0 comments

I am trying to use this in an API call:


        forEach(["a", "b", "c"], function(item, index, arr) {
            var done = this.async();
        console.log("each", item, index, arr);
              setTimeout(function() {
              done();
            }, 500);
          }, function(){
              console.log("all done demo")
          }
        );
        
          console.log("END OF FOREACH")

here's what console looks like:

 each a 0 [ 'a', 'b', 'c' ]

 END OF FOREACH  //as soon as it reaches this point, it returns to the calling function. 

each b 1 [ 'a', 'b', 'c' ]

each c 2 [ 'a', 'b', 'c' ]

I essentially wanted to calculate some values in the foreach callback and return to calling the function from there. How do I get that working?

simran66 avatar Nov 28 '17 07:11 simran66