让我先说一下,我已经搜索了很多类似的问题,但还没有遇到一个包含相同逻辑的问题。
我的代码中有一个复杂的ajax请求链,构成了项目部署工具的一部分。项目由一组用户组和数据层组成。
一个组可以有多个数据层,可以有多个组。
的例子:
纽约:['Police', 'Ambulance', 'Fire Department']
Boston: ['Police', 'Ambulance', 'Fire Department']
每个数据层都是保存记录的主项目数据集的"视图"。数据层控制显示哪些记录(通过SQL查询)以及其中包含哪些字段。
我在使用承诺等待ajax请求完成后再进行下一步时遇到了麻烦。
我遇到的主要问题是for循环导致promise集合中断-下一步将在一个组的图层创建后触发,而它应该在所有组的图层创建后才触发。
请参阅我的伪代码如何javascript代码看起来:
// A user can choose if data layers for each group of users is split
// between groups or shared between all groups. Data layers are then created
// based on this option.
userGroups = ['New York', 'Boston'];
userGroupPolicy = 'grouped' OR 'not grouped'; // depending on what user chooses
layerTypes = ['Police', 'Ambulance', 'Fire Department'];
if (userGroups are not grouped){
for (each userGroup in userGroups){
createLayers(userGroup, layers);
}
}
else if (userGroups are grouped){
createLayers(userGroups.join(", "), layers);
}
// when the createLayers function and sub functions have
// created/defined/added fields for the 6 layers
// continue with the createFolder function.
// Only one folder is ever needed for the project, no matter
// if the user groups are split or not, so the function should
// only run once and after all layers are completed.
when all promises have resolved({
createFolder();
}):
// ------------------------------------------------------------
// Below is the createEmptyLayer function and sub functions.
// There are multiple layers that need to be created that
// can vary between 2 and 8 per group depending on what the user selects.
// The layers are linked to an already created dataset that holds all
// data.
function createEmptyLayers(userGroup, layers){
for (each layer in layers){
params = define params for layer; // (name, type, max record count...);
$.ajax request to create the layer per layer type {
layerParams: params
}
.done(
// Now add the definition to the layer (definition query, capabilities, link it to dataset)
addAttrToLayer(result from createEmptyLayers);
);
}
}
function addAttrToLayer(result){
attributes = bunch of definitions and linkages;
$.ajax request to add the parameters to the layer{
defineAttributes: attributes
}
.done(
// Now the layer exists and is set to the dataset with all attributes required.
// we can now choose what fields from the dataset will show in it.
addFieldsToLayer(result from addAttrToLayer);
)
}
function addFieldsToLayer(result){
// some logic here defines the fields by
// layer type from an ajax request to the main layer
// (occurs prior to this chain of functions);
$.ajax request to the layer {
someAttr: result,
fields: fieldsToShow
}
.done(
return the result to the promise array
)
}
我怎么能等待6个结果完成之前运行的createFolder函数使用承诺?
许多谢谢。
尝试Promise.all
方法等待所有的输入承诺
您可以使用.then()
将所有promise请求以顺序的方式链接起来。
这个.then()
语法同时支持jQuery和AngularJS
代码示例
function first() {
return $.ajax(...);
}
function second(data, textStatus, jqXHR) {
return $.ajax(...);
}
function third(data, textStatus, jqXHR) {
return $.ajax(...);
}
function main() {
first().then(second).then(third);
}
代码样本取自此答案我如何链三个异步调用使用jQuery承诺?