Async call in javascript For Loop not working

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, 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;

self.downloadDailyExcelProcurement = function (filters, excelTmpArr) {
                self.disableExcelDownloadProcurement(true);
                $('.useCaseExcelButtonProcurement .oj-button-button .oj-button-label')[0].style.backgroundColor = "gray";
                $('.useCaseExcelButtonProcurement .oj-button-button .oj-button-label .demo-download-icon-24')[0].style.color = "#D8D8D8";
                var payload = {};
                if (typeof filters === "string") {
                    var fill = filters;
                } else {
                    var fill = self.sendFilters();
                    if(self.app() === "fusion"){
                        fill += '&module=Procurement';
                    }else if (self.app() === "o2r"){
                        fill += '&module=O2r';
                    }
                }
                if(fill.includes("%3A")){
                    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'));
                    $(".custom-loader-circle").show();
                };
                payload.OnSuccess = function (response) {
                    var results = response.results;
                    for (var res in results) {
                        var obj = {}
                        obj.ferp = results[res].name;
                        obj.po = "NA"
                        obj.receipt_no = "NA"
                        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 = "NA";
                                  }
                                
                                  self.excelTmpArr().push(obj);
                              });
                         }
                          else {
                             self.excelTmpArr().push(obj);
                        }
                    }
                    if (response.next) {
                        filters = ((response.next).split('testcollection'))[1];
                        if (filters[0] === "/") {
                            var test = filters.slice(1, filters.length);
                        }
                        self.downloadDailyExcelProcurement(test, self.excelTmpArr());
                    } else {
                        if (results.length === 0) {
                            $(".custom-loader-circle").hide();
                            self.disableExcelDownloadProcurement(false);
                            $('.useCaseExcelButtonProcurement .oj-button-button .oj-button-label')[0].style.backgroundColor = "#4d0000";
                            $('.useCaseExcelButtonProcurement .oj-button-button .oj-button-label .demo-download-icon-24')[0].style.color = "white";
                            showMessage(self.messages, "No Data to Download", '', 'info');
                        } else {
                            
                            self.formatForExcel(self.excelTmpArr(), fill, "Procurement");
                        }
                    }
                };
                payload.OnError = function (data) {
                    showMessage(self.messages, data.status, data.statusText, 'error');
                    $(".custom-loader-circle").hide();
                };
                getData(payload);
            }

67 thoughts on “Async call in javascript For Loop not working”

  1. 645517 713368If youre still on the fence: grab your favorite earphones, head down to a Best Buy and ask to plug them into a Zune then an iPod and see which 1 sounds better to you, and which interface makes you smile far more. Then youll know which is correct for you. 863522

    Reply

Leave a Comment