I have been attempting to implement the AngularJS promise/then functionality with a recursive function, but I am facing an issue where the then-function is not being called. None of the error-, success-, or notify-callbacks are getting triggered.
Below is the code snippet:
Recursive Function
loadSection2 = function() {
var apiURL = "http://..."
var deferred = $q.defer();
$http({
method: "GET",
url: apiURL
}).success(function(result, status, headers, config) {
console.log(result);
loadCount++;
if(loadCount < 10) {
newSectionArray.push(result);
loadSection2();
} else {
loadCount = 0;
deferred.resolve();
return deferred.promise;
}
}).error(function() {
return deferred.reject();
});
deferred.notify();
return deferred.promise;
};
Then-Function
loadSection2().then(function() {
console.log("NEW SECTIONS LOADED, start adding to document");
addContent();
}, function() {
console.log("ERROR CALLBACK");
}, function() {
console.log("NOTIFY CALLBACK");
}).then(function() {
loadScrollActive = false;
});
It seems like the then should trigger the initial notify-callback at least. However, no callbacks are being invoked. Could it be that then does not work well with recursive functions?