-2

The code below does not throw a syntax error, but the THEN statement which renders the data on the datatable is executing first, before all lists have been queried

What is the intention of this code, there are many sharepoint lists with similar names: Bill Cycles, Bill Cycles Archive1....N.

I need to query all those lists for a specific query, and concatenate the results of all of them in the result variable and then use that variable with datatables plugin to render, but as explain on the first statement, the outer .then is executeed in the beginning.

function GetData(billCycleId, clientCode, jobCodes, engagementCode) {
    var deferred = $q.defer();
    var enhanceFunctions = [
        function(searchResultRow) {
            return spService.AddHyperLinkOnFields(searchResultRow, config.HyperLinks);
        },
        function(searchResultRow) {
            return spService.AddPresenceOnFields(searchResultRow, config.UserFields);
        },
        function(searchResultRow) {
            return spService.FormatDateFields(searchResultRow, config.DateFields, generalConfig.DateTimeFormat);
        },
        function(searchResultRow) {
            return spService.AddImageMapping(searchResultRow, config.ImageFields);
        },
        function(searchResultRow) {
            return spService.FormatNumberFields(searchResultRow, config.NumberFields);
        }
    ];

    var selectProperties = spService.TransformFieldsToSelectProperties(config.Fields); 
    var extendedSelectProperties = selectProperties.slice(); // copy array
    var hyperLinkedProperties = spService.TransformFieldsToSelectProperties(config.HyperLinks)
    extendedSelectProperties = extendedSelectProperties.concat(hyperLinkedProperties);
    var result =[];
    spService.GetAllListsFromWeb()
    .then(function (lists) {
      var listEnumerator = lists.getEnumerator();
      return $q.all(
        (function(){
          //var promises = [];
          while (listEnumerator.moveNext()) {
            var oList = listEnumerator.get_current();
            var title = oList.get_title();
            var id = oList.get_id();
            if (title.indexOf('Bill Cycles') !== -1) {
              // Get data from SP !!! this is also async and returns a promise
              //   add the promise to promises array and wait for all to finish
              //   look above in Promise.all
              //promises.push(
                GetRelatedBillCyclesFromList(
                  id, 
                  extendedSelectProperties, 
                  billCycleId, 
                  clientCode, 
                  jobCodes, 
                  engagementCode, 
                  enhanceFunctions
                )
                .then(function (data) {
                  var trimmedData = 
                    spService
                    .SpSearchQuery
                    .TrimSearchResultsToSelectProperties(
                      data, 
                      selectProperties
                    );

                    trimmedData.forEach(function(item){ // loop over source array
                        result.push(item); //append to result array
                    });
                })
              //);
            }
          }
          //return promises
        })() //IIFE returning an array of promises
      );
    })
    .then( //The promise is used to execute something after all promises are resolved, but the results are build in the result array, no need to use data
      function(data){
        //var resultadata = data;
        var dataTable = $(tableSelector).DataTable();
        dataTable.clear().rows.add(result).columns.adjust().draw(); // Resize columns based on new data sizes              
        vm.ValidDataLoaded = true;    
      }
    );

}

Update 1

function GetData(billCycleId, clientCode, jobCodes, engagementCode) {
                    var deferred = $q.defer();
                    var enhanceFunctions = [
                        function(searchResultRow) {
                            return spService.AddHyperLinkOnFields(searchResultRow, config.HyperLinks);
                        },
                        function(searchResultRow) {
                            return spService.AddPresenceOnFields(searchResultRow, config.UserFields);
                        },
                        function(searchResultRow) {
                            return spService.FormatDateFields(searchResultRow, config.DateFields, generalConfig.DateTimeFormat);
                        },
                        function(searchResultRow) {
                            return spService.AddImageMapping(searchResultRow, config.ImageFields);
                        },
                        function(searchResultRow) {
                            return spService.FormatNumberFields(searchResultRow, config.NumberFields);
                        }
                    ];

                    var selectProperties = spService.TransformFieldsToSelectProperties(config.Fields); 
                    var extendedSelectProperties = selectProperties.slice(); // copy array
                    var hyperLinkedProperties = spService.TransformFieldsToSelectProperties(config.HyperLinks)
                    extendedSelectProperties = extendedSelectProperties.concat(hyperLinkedProperties);
                    var result =[];
                    spService.GetAllListsFromWeb()
                    .then(function (lists) {
                      var listEnumerator = lists.getEnumerator();
                      return $q.all(
                        (function(){
                          var promises = [];
                          while (listEnumerator.moveNext()) {
                            var oList = listEnumerator.get_current();
                            var title = oList.get_title();
                            var id = oList.get_id();
                            if (title.indexOf('Bill Cycles') !== -1) {
                              // Get data from SP !!! this is also async and returns a promise
                              //   add the promise to promises array and wait for all to finish
                              //   look above in Promise.all
                              promises.push(
                                GetRelatedBillCyclesFromList(
                                  id, 
                                  extendedSelectProperties, 
                                  billCycleId, 
                                  clientCode, 
                                  jobCodes, 
                                  engagementCode, 
                                  enhanceFunctions
                                )
                                .then(function (data) {
                                  var trimmedData = 
                                    spService
                                    .SpSearchQuery
                                    .TrimSearchResultsToSelectProperties(
                                      data, 
                                      selectProperties
                                    );

                                    trimmedData.forEach(function(item){ // loop over source array
                                        result.push(item); //append to result array
                                    });
                                })
                              );
                            }
                          }
                          //return promises
                        })() //IIFE returning an array of promises
                      );
                    })
                    .then( //The promise is used to execute something after all promises are resolved, but the results are build in the result array, no need to use data
                      function(data){
                        //var resultadata = data;
                        var dataTable = $(tableSelector).DataTable();
                        dataTable.clear().rows.add(result).columns.adjust().draw(); // Resize columns based on new data sizes              
                        vm.ValidDataLoaded = true;    
                      }
                    );

                }
Luis Valencia
  • 32,619
  • 93
  • 286
  • 506

1 Answers1

1

The issue is your (function(){ does not return promises. Actually, it returns nothing.

But the code you commented is the correct solution. you create an array of promises, then you use $q.all(promises).then(function(){data}...)

Arashsoft
  • 2,749
  • 5
  • 35
  • 58