Exploring the ins and outs of angular resource, I'm baffled by its behavior in this scenario. Let's take a look at the code snippet below (assuming that the query function is functioning correctly):
$scope.getSomething = function (someObject) {
var result;
Factory.getSomething.query({ id: someObject.id }).$promise.then(function (success) {
alert('success');
result = 'success';
},
function(error) {
alert('error');
result = 'error';
});
alert(result);
};
Upon executing this method, I observe the following sequence: 1) Alert displaying 'undefined' 2) Alert showing 'success'
Why does 'undefined' appear first? My understanding of $promise suggests that
alert('success');
result = 'success';
should be triggered initially upon success (in asynchronous execution).
Subsequently, the last alert should display the value of result
.
However, it appears to be behaving differently than anticipated.
Any insights or recommendations? Thank you in advance.