I have a REST service offering a list of "Json" objects, and each object can have a link to another resource of its class. Starting with a specific one, I need to get them all by making a recursive HTTP call. Therefore, I wrote:
var steps = [];
var recursiveLookup = function(processId) {
return $.ajax({
url: SERVER_URL + processId,
success: function (activity) {
var rtn = activity.path.map(step => {
if (step.type != "Node") {
steps.push(step);
} else {
return recursiveLookup(step.subProcessIntanceId);
}
}).filter(a => a != undefined);
return rtn;
}
});
}
This would correctly load all objects into the var global steps. I have to be sure that the method is finished, so I wrote:
var promises = recursiveLookup(processId);
Promise.all(promises).then(function () {
console.log(steps);
});
But it does not work, because "recursiveLookup" returns the promise of $ .ajax instead of a set of promises pretending to be returned using the success method.
Also, is it possible to get steps as a return value from the "recursiveLookup" method, using it as a global variable?
source
share