First function will be executed if request returns with 200 second function in any error case.
If you building an own promise around it -> return the promise and you’ve got it.
An example:
Service
angular
.module('services.example')
.factory('ExampleService', function($scope, $http, $q) {
var result = [];
return {
getData: function() {
// create new promise/queue
var promise = $q.defer();
$http.get('YOUR_URL').then(function (repsonse) {
// success
promise.resolve(response);
}, function (response) {
// fail
promise.reject(response);
});
// return promise
return promise.promise;
}
};
});
It is a very nice solution if you have a generic request service, that handles every request with a generic error handler.
So you have write this code 1 time and not for each request you send ;).
And you can build up arrays of request -> and promise all together with $q.all(ArrayOfPromises) -> so you can send different request but only get success or error if all have finished.