简体   繁体   English

在while循环中链接promise不能正确返回值

[英]Chaining promises in while loop not correctly returning values

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 下面的代码不会引发语法错误,但是在查询所有列表之前,首先执行将数据呈现到数据表上的THEN语句

What is the intention of this code, there are many sharepoint lists with similar names: Bill Cycles, Bill Cycles Archive1....N. 该代码的用途是,有许多名称相似的共享点列表: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 and inner then is executed later when the page is already rendered. 我需要查询所有这些列表以进行特定查询,然后将所有列表的结果连接到result变量中,然后将该变量与datatables插件一起使用来呈现,但是如第一条语句所述,外部.then在当页面已经渲染时,开始和内部然后在以后执行。

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;    
                      }
                    );

                }

You pass undefined to $q.all , because //return promises is commented out. 您将undefined传递给$q.all ,因为//return promises已被注释掉。 Uncommenting that should fix your issue. 取消注释应该可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM