In specific case where we execute asynchronous call inside the if
block, how come the else
statement block gets executed if doSubmit
evaluates to true
? Outcome of this is that it always ends up at line after the ERROR_2
comment:
$scope.save = function() {
if (doSubmit) {
var dto = {
'attributes': $scope.fields,
'requestOrigin': $location.absUrl()
};
var req = {
method: 'POST',
url: 'endpoint',
data: dto
};
$http(req).success(function(data, status) {
// SUCCESS
$scope.completed(data);
}).error(function(data, status) {
// ERROR_1
$scope.validationFailed();
});
} else {
// ERROR_2
$scope.validationFailed();
}
}
// Used to provide error messages about input validation
$scope.validationFailed = function(message, id) {
$scope.alerts.push({
type: 'danger',
msg: message || 'error.validation_failed', id: id || 'general'
});
}
I read about this case somewhere but can't remember the source. Any link or short elaboration would be helpful.
ADDITION 1 $scope.validationFailed