My JavaScript application requires making an ajax call and potentially more calls based on the previous response. Currently, I have implemented this using a somewhat cumbersome pyramid of doom:
function startParentArray(id) {
getIssueDetail(id).success(function(data) {
var source = $("#parenttemplate").html();
var tpl = Handlebars.compile(source);
processResponse(data, tpl);
});
}
function processResponse(data, tpl) {
if (data.issue.parent) {
nextparent = data.issue.parent.id;
makeAjaxCall(nextparent, tpl);
}
}
function makeAjaxCall(parentId, tpl) {
getIssueDetail(parentId).success(function(data) {
$("#parenttable").append(tpl(data.issue));
if (data.issue.parent) {
makeAjaxCall(data.issue.parent.id, tpl);
}
});
}
Is there a cleaner way to achieve this behavior, perhaps using a while-loop or other approach?