Javascript 递归的;获取API";全部完成后回调
我的代码:Javascript 递归的;获取API";全部完成后回调,javascript,jquery,ajax,Javascript,Jquery,Ajax,我的代码: function genTree(parent_id) { GetData('tableName', { 'parent': parent_id }).then(function(features) { var feature = null for (var i = 0, total = features.length; i < total; i++) { feature = feature
function genTree(parent_id) {
GetData('tableName', {
'parent': parent_id
}).then(function(features) {
var feature = null
for (var i = 0, total = features.length; i < total; i++) {
feature = features[i]
var temp = {}
var parentIndex = getIndexById(parent_id);
temp["ID"] = feature['ID']
temp["PARENT_ID"] = feature['PARENT_ID'] || 0
state.splice(parentIndex + (i + 1), 0, temp)
genTree(feature['ID']);
}
}).catch(function(e) {
console.log('Error', e)
})
}
函数genTree(父项id){
GetData('tableName'{
“家长”:家长id
}).然后(功能(特性){
var功能=空
对于(var i=0,total=features.length;i
getData()是一个使用“Fetch API”并将响应xml解析为数组的函数。我无法编辑此函数
它工作得很好,但在所有数据都已加载的情况下,我很难进行回调。我正在寻找最好的解决方案。如果您返回您创建的所有承诺,并将它们提供给
Promise.all
,那么您的主函数调用将返回一个承诺,该承诺将在所有问题都得到解决时得到解决:
function genTree(parent_id) {
return GetData('tableName', {
parent: parent_id
}).then(function(features) {
var parentIndex = getIndexById(parent_id);
return Promise.all(
features.map(function (feature, i) {
var temp = {
ID: feature.ID,
PARENT_ID: feature.PARENT_ID || 0
};
state.splice(parentIndex + (i + 1), 0, temp);
return genTree(feature.ID);
})
);
}).catch(function(e) {
console.log('Error', e);
})
}
现在您可以执行以下操作:
genTree(parent_id).then(function () {
console.log('all done');
});
注意,我没有接触到state.splice(parentIndex+(I+1),0,temp)代码>,因为不清楚什么是状态
,以及如何使用它,但我确实想知道这是否是您所需要的,因为拼接
将在现有数组元素的索引大于此处给出的索引时将其向右移动。考虑替代:
state[parentIndex + i + 1] = temp;
如果返回所创建的所有承诺,并将其馈送到
Promise.all
,则主函数调用将返回一个承诺,该承诺将在所有承诺都已解决时得到解决:
function genTree(parent_id) {
return GetData('tableName', {
parent: parent_id
}).then(function(features) {
var parentIndex = getIndexById(parent_id);
return Promise.all(
features.map(function (feature, i) {
var temp = {
ID: feature.ID,
PARENT_ID: feature.PARENT_ID || 0
};
state.splice(parentIndex + (i + 1), 0, temp);
return genTree(feature.ID);
})
);
}).catch(function(e) {
console.log('Error', e);
})
}
现在您可以执行以下操作:
genTree(parent_id).then(function () {
console.log('all done');
});
注意,我没有接触到state.splice(parentIndex+(I+1),0,temp)代码>,因为不清楚什么是状态
,以及如何使用它,但我确实想知道这是否是您所需要的,因为拼接
将在现有数组元素的索引大于此处给出的索引时将其向右移动。考虑替代:
state[parentIndex + i + 1] = temp;
“当现有数组元素的索引大于此处给定的索引时,splice会将其向右移动”我希望我的状态数组中的所有项都按顺序排列:所有子项都位于父项的右侧(紧挨着父项)。这是故意的,但我不确定这是最好的方法。如果它对你有效,那就保持原样(反正这不是问题的主题)。如果这成为一个问题,我建议您专门提出一个新的问题,但是您还应该提供一个您处理的数据示例,特别是
feature.ID
是什么,以及您希望如何在状态下找到某些条目例如:genTree(mainProces_id).then(function(){});以及我的错误:无法读取UndefinedId的属性'then',您将所有3return
关键字添加到我放置它们的位置?“当现有数组元素的索引大于此处给定的索引时,splice会将它们向右移动”我希望我的状态数组中的所有项都按顺序排列:所有子项都位于父项的右侧(旁边)。这是故意的,但我不确定这是最好的方法。如果它对您有效,请保持原样(无论如何,这不是问题的主题)。如果这成为一个问题,我建议您专门提出一个新的问题,但是您还应该提供一个您处理的数据示例,特别是feature.ID
是什么,以及您希望如何在状态下找到某些条目比如:genTree(mainProces_id).then(function(){});以及我的错误:无法读取undefineded的属性'then',您将所有3return
关键字添加到我放置它们的位置?