From your code:
simulateAjax
is a function returns promise. Next, you call this method in success
callback.
Therefore ListOfTags
returns promise as well.
I would implement it as chain promise instead to mix success
callback with then()
- You can chain them to create code flows
- Error propagates, so you can catch it on the end of the chain
- Actually, they are much like an asynchronous equivalent for the well-known try-catch-finally clause.
Example
$scope.ListOfTags = function() {
var myHttp = $http({method: 'Post', url: 'FillCategories'});
myHttp.then(function (result) {
return simulateAjax(data);
}).then(function (result) {
return result;
}, function (result) {
alert("Error: No data returned");
});
}
If you interesting to change your code try followed way (but I still prefer upper case):
$scope.ListOfTags = function() {
$http({
method: 'Post',
url: 'FillCategories'
}).success(function (data, status, headers, config) {
$scope.optionsFromQuery = (function () {
return simulateAjax(data).then(function (result) {
return result;
}, function (result) {
alert("Error: No data returned");
return null;
});
})();
}).error(function (data, status, headers, config) {
$scope.message = 'Unexpected Error';
});
}