I am developing an app on MEAN stack. I made factory to handle my http requests and then using that service in my controller.
service
.factory('dataSvc', function($q, $http, $timeout, $rootScope, $resource) {
var dataSvc = {};
dataSvc.list = function(options) {
var defer = $q.defer();
return $http.get(options.url).success(function(data) {
console.log('========data========', data)
defer.resolve(data);
}).error(function(err){
console.log('======err=======', err)
defer.reject(err);
});
return defer.promise;
}
return dataSvc;
});
I have two questions regarding this:
even if I get 401 status from backend I always end up in success function, So what I am doing wrong here.
also If I remove return from $http.get then the functionality breaks. So What is the need for this return keyword in front of $http.get
controller
dataSvc.list({url: '/api/users'}).then(function(result) {
console.log('result here===========', result);
if(result.status == 401) {
}
else {
}
});
and also what is the difference between then method and success and error methods provided by angular ?