title: Async call in javascript For Loop not working
body: <p>I have a callback function inside a loop here for (var res in results) {
  but it seems the loop is not waiting for the async call. When I am calling self.callTestOutputData(test_output_url) here, <strong>the loop is not waiting fpor the response but continuing for the next iteration and I am losing out the value to push into obj.requistion_number = testOutputResponse.value;
  Please note : var results = response.results   Here results is an array of Json objects.</strong></p>
  <p>Edit 1 : I tried forEach but that didn't work .</p>
  <pre><code>results.forEach(res =&gt; {
    var obj = {}
    obj.ferp = res.name;
    // your code...
  })
  </code></pre>
  <p>Original Code:</p>
  <pre><code>self.downloadDailyExcelProcurement = function (filters, excelTmpArr) {
                  self.disableExcelDownloadProcurement(true);
                  $('.useCaseExcelButtonProcurement .oj-button-button .oj-button-label')[0].style.backgroundColor = &quot;gray&quot;;
                  $('.useCaseExcelButtonProcurement .oj-button-button .oj-button-label .demo-download-icon-24')[0].style.color = &quot;#D8D8D8&quot;;
                  var payload = {};
                  if (typeof filters === &quot;string&quot;) {
                      var fill = filters;
                  } else {
                      var fill = self.sendFilters();
                      if(self.app() === &quot;fusion&quot;){
                          fill += '&amp;module=Procurement';
                      }else if (self.app() === &quot;o2r&quot;){
                          fill += '&amp;module=O2r';
                      }
                  }
                  if(fill.includes(&quot;%3A&quot;)){
                      fill = fill.replace(/%3A/g, ':');
                  }
                  payload.Endpoint = 'executions/testcollection/' + fill;
                  //console.log(payload.Endpoint)
                  payload.BeforeSend = function (xhr) {
                      xhr.setRequestHeader('Authorization', 'Basic ' + btoa('guest:oracle123'));
                      $(&quot;.custom-loader-circle&quot;).show();
                  };
                  payload.OnSuccess = function (response) {
                      var results = response.results;
                      for (var res in results) {
                          var obj = {}
                          obj.ferp = results[res].name;
                          obj.po = &quot;NA&quot;
                          obj.receipt_no = &quot;NA&quot;
                          var test_output_url =  results[res].reference_test_cases[0].automation_tests[0].test_outputs[0]
           
                                $.when(self.callTestOutputData(test_output_url)).done(function (testOutputResponse) {
                                    if(testOutputResponse)
                                    obj.requistion_number = testOutputResponse.value;
                                    else {
                                       obj.requistion_number = &quot;NA&quot;;
                                    }
                                  
                                    self.excelTmpArr().push(obj);
                                });
                           }
                            else {
                               self.excelTmpArr().push(obj);
                          }
                      }
                      if (response.next) {
                          filters = ((response.next).split('testcollection'))[1];
                          if (filters[0] === &quot;/&quot;) {
                              var test = filters.slice(1, filters.length);
                          }
                          self.downloadDailyExcelProcurement(test, self.excelTmpArr());
                      } else {
                          if (results.length === 0) {
                              $(&quot;.custom-loader-circle&quot;).hide();
                              self.disableExcelDownloadProcurement(false);
                              $('.useCaseExcelButtonProcurement .oj-button-button .oj-button-label')[0].style.backgroundColor = &quot;#4d0000&quot;;
                              $('.useCaseExcelButtonProcurement .oj-button-button .oj-button-label .demo-download-icon-24')[0].style.color = &quot;white&quot;;
                              showMessage(self.messages, &quot;No Data to Download&quot;, '', 'info');
                          } else {
                              
                              self.formatForExcel(self.excelTmpArr(), fill, &quot;Procurement&quot;);
                          }
                      }
                  };
                  payload.OnError = function (data) {
                      showMessage(self.messages, data.status, data.statusText, 'error');
                      $(&quot;.custom-loader-circle&quot;).hide();
                  };
                  getData(payload);
              }
  </code></pre>