gpt4 book ai didi

javascript - ajax调用中的厄运金字塔

转载 作者:行者123 更新时间:2023-12-03 10:32:41 24 4
gpt4 key购买 nike

我有一个 javascript 应用程序,我想在其中进行 ajax 调用,并且如果需要,根据之前的 ajax 响应进行其他 ajax 调用。我成功地用一个漂亮的厄运金字塔做到了这一点:

    function startParentArray(id) {
getIssueDetail(id).success(function(data) {
var source = $("#parenttemplate").html();
var tpl = Handlebars.compile(source);
if(data.issue.parent) {
nextparent = data.issue.parent.id;
getIssueDetail(nextparent).success(function(data1) {
$("#parenttable").append(tpl(data1.issue));
if(data1.issue.parent) {
nextparent = data1.issue.parent.id;
getIssueDetail(nextparent).success(function(data2) {
$("#parenttable").append(tpl(data2.issue));
if(data2.issue.parent) {
nextparent = data2.issue.parent.id;
getIssueDetail(nextparent).success(function(data3) {
$("#parenttable").append(tpl(data3.issue));
if(data3.issue.parent) {
nextparent = data3.issue.parent.id;
getIssueDetail(nextparent).success(function(data4) {
$("#parenttable").append(tpl(data4.issue));
if(data4.issue.parent){
nextparent = data4.issue.parent.id;
getIssueDetail(nextparent).success(function(data5) {
$("#parenttable").append(tpl(data5.issue));
});
}
});
}
});
}
});
}
});
}
});

}

getIssueDetail 返回 ajax 响应。有没有其他更漂亮的方法来做到这一点,例如在 while 循环中?

最佳答案

promise 就是你正在寻找的,我正在考虑this one 。但 jQuery 也有一个 ($.Deferred 即使它没有实现所谓的 A+ promises

这来自 kris kowal 的 q

On the first pass, promises can mitigate the “Pyramid of Doom”: the situation where code marches to the right faster than it marches forward.

关于javascript - ajax调用中的厄运金字塔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29140719/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com