在 while 循环中链接承诺不正确返回值
Chaining promises in while loop not correctly returning values
下面的代码没有抛出语法错误,但是在查询所有列表之前首先执行在数据表上呈现数据的 THEN 语句
此代码的意图是什么,有许多名称相似的共享点列表:Bill Cycles、Bill Cycles Archive1....N.
我需要为特定查询查询所有这些列表,并将所有列表的结果连接到结果变量中,然后使用该变量和数据表插件来呈现,但正如第一个语句中所解释的那样,外部.then 在开头执行,内部 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;
}
);
}
您将 undefined
传递给 $q.all
,因为 //return promises
被注释掉了。取消注释应该可以解决您的问题。
下面的代码没有抛出语法错误,但是在查询所有列表之前首先执行在数据表上呈现数据的 THEN 语句
此代码的意图是什么,有许多名称相似的共享点列表:Bill Cycles、Bill Cycles Archive1....N.
我需要为特定查询查询所有这些列表,并将所有列表的结果连接到结果变量中,然后使用该变量和数据表插件来呈现,但正如第一个语句中所解释的那样,外部.then 在开头执行,内部 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;
}
);
}
您将 undefined
传递给 $q.all
,因为 //return promises
被注释掉了。取消注释应该可以解决您的问题。