I want to stop repeated call to getModelData
if shouldPermit
becomes true
. But below code always prints why I got printed forEach newlySelectedUsers
in console for each newlySelectedUsers
.
var shouldPermit = false;
angular.forEach(newlySelectedUsers, function(value, key) {
if(!shouldPermit) {
console.log("why I got printed forEach newlySelectedUsers")
userService.getModelData($scope.$root.test[value].id)
.success(function (data) {
if(lodash.isEmpty(data)) {
shouldPermit = true;
$scope.insertSaving();
}
});
}
});
How to stop calls to getModelData
once shouldPermit
becomes true?
That's because all the http requests run asynchronusly. That beeing said your itteration runs faster than the time needed for service to find (in your case) an empty data variable .
An option is to recursively call the http request: