【发布时间】:2018-10-11 09:34:39
【问题描述】:
我有以下代码:
$.when(multipleQueries(stateQueries, rowData))
.then(function (data) {
//do stuff with data - removed for brevity
multipleQueries 函数如下:
function multipleQueries(queriesToExecute, rowData) {
var allQueriesMapped = $.Deferred();
// If a single query has been provided, convert it into an array
if (Array.isArray(queriesToExecute) === false) {
queriesToExecute = [].concat(queriesToExecute);
}
// Create a function for each region to run the query.
$.when.apply($, $.map(queriesToExecute, function (query) {
// Execute the query in the region
return $.when(executeQuery(query.InstanceInfo, query.Query)).then(function (data) {
var isDataMapped = $.Deferred();
var mappedData = [];
// Perform some data transformation
$.when.apply($, $.map(data.results, function (value) {
var properties = value.succinctProperties;
//code removed for brevity
return $.when(mapData(properties)).then(function (mappedRow) {
if (mappedRow) {
mappedData.push(mappedRow);
}
});
})).then(function () {
isDataMapped.resolve({
results: mappedData,
numItems: mappedData.length
});
});
return isDataMapped.promise();
}).then(function (data) {
debugger;
allQueriesMapped.resolve(data);
});
}));
return allQueriesMapped.promise();
}
我遇到的问题是,我传入了 5 个查询以执行到 multipleQueries 函数,但在运行第一个查询后它正在运行调试器行 - 然后解决 allQueriesMapped deferred,然后它返回到 do包含调用它的数据的东西,但是因为我没有来自我传入的 5 个 queires 的所有数据,所以我没有看到预期的行为 - 我如何设置这些承诺有什么遗漏吗?
注意 - 我尝试将调试器之前的 .then 更改为 .done 但得到相同的行为,并且还尝试将调用代码更改为 .done 如下所示但也得到相同的结果。
$.when(multipleQueries(stateQueries, rowData))
.done(function (data) {
//do stuff with data - removed for brevity
** 更新——执行查询函数如下
function executeQuery(instanceInfo, query) {
return $.ajax({
url: instanceInfo.Url,
type: 'GET',
data: {
q: query,
succinct: true
},
processData: true
});
}
【问题讨论】:
-
看起来它在第一个
then中遇到了您的调试器行,因为then附加到循环中的每个$.when。即when(A,B,C) { when(a).then(a), when(b).then(b), when(c).then(c) } -
@T.J.Crowder - 更新了问题以包含该功能
-
then()和debugger处于错误级别。它在第一个map()内。把它移出那个循环 -
@TJCrowder 不用担心 - 从所有嵌套的 when/then/when/when/maybe then/ 不清楚这是否 是 答案(只是看起来错了)。
标签: javascript jquery promise jquery-deferred