I need to execute a function after the recursive asynchronous ajax calls. I have:
var tree = [1, 2 ,3, 4];
recursion(0);
function recursion(i) {
$.ajax('http://example.com/' + tree[i])
.done(function (data) {
// data is array
++i;
if (tree[i] !==undefined) {
$.each(data, function () {
recursion(i);
});
}
});
}
And I want after all calls when they are done to do:
recursion(0).done(function () {alert('All calls are done!')});
I know, I should use $.Deferred of JQuery, but ajax call return promise too. I'm trying to use $.Deferred but I encountered a problem with loop in this place:
$.each(data, function () {
recursion(i);
});
Please, help me.