I am using AngularJS, I have a function in a service with nested promises:
this.getOfferStatus = function (offer) {
if(offer.isDraft){
return 'Draft';
} else {
this.isProcessed(offer).then(function (isProcessed) {
if (isProcessed) {
this.isAccepted(offer).then(function (isAccepted) {
if (isAccepted) {
if (isExpired(offer)) {
return 'Expired';
} else {
if (this.isActive(offer).then(function (isActive) {
return 'Active';
}, function (err) {
console.error(err);
}));
}
} else {
return 'Rejected';
}
}, function (err) {
console.error(err);
})
} else {
return 'Pending';
}
}, function (err) {
console.error(err);
});
}
}
}
But, when I call this function in my controller, I get this error:
XHR finished loading: GET "http://localhost:8080/localbusiness/1/offers/Absolute". Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting! Watchers fired in the last 5 iterations: [] http://errors.angularjs.org/1.5.0/$rootScope/infdig?p0=10&p1=%5B%5D at angular.js:68 at Scope.$digest (angular.js:16702) at Scope.$apply (angular.js:16928) at done (angular.js:11266) at completeRequest (angular.js:11464) at XMLHttpRequest.requestLoaded (angular.js:11405) Uncaught Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting! Watchers fired in the last 5 iterations: []
isProcessed() function:
this.isProcessed = function (offer) {
var deferred = $q.defer();
var LocalBusinessResource =
$resource(apiService + '/offers/:id/processed', {id: '@id'});
LocalBusinessResource.get({id: offer.id}, function (result) {
deferred.resolve(result);
}, function (err) {
return $q.reject(err);
alert('check your server connection ' + angular.toJson(err));
});
return deferred.promise;
}
In my controller:
$scope.getStatus = function(offer){
return offerService.getOfferStatus(offer);
};
isProcessed()do? I suspect that is creating an infinite recursive loop. Also using thethiskeyword inside a fulfillment handler of a.thenmethod won't work. The promise spec says that thethiskeyword should be undefined in strict mode and the global context in sloppy mode.