首先讓我說我已經為類似的問題做了很多搜索,但沒有遇到包含相同邏輯的問題。
我的代碼中有一個復雜的 ajax 請求鏈,它們構成了專案部署工具的一部分。一個專案由一組用戶組和資料層組成。
一個組可以有多個資料層,也可以有多個組。
例子:
紐約:[“警察”、“救護車”、“消防局”]
波士頓:['警察'、'救護車'、'消防局']
每個資料層都是保存記錄的主專案資料集的“視圖”。資料層控制顯示哪些記錄(通過 SQL 查詢)以及其中包含哪些欄位。
在繼續下一步之前,我無法使用 promise 等待 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
)
}
在使用 Promise 運行 createFolder 函式之前,如何等待 6 個結果完成?
非常感謝。
uj5u.com熱心網友回復:
tryPromise.all方法等待所有輸入承諾
uj5u.com熱心網友回復:
您可以使用.then()以順序方式鏈接所有要呼叫的承諾請求。
此.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 承諾鏈接三個異步呼叫?
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/330896.html
標籤:javascript 查询 阿贾克斯
