title: using factory service for http calls and using them through controller
body: <p>I am developing an app on MEAN stack. I made factory to handle my http requests and then using that service in my controller.</p>
  
  <p>service</p>
  
  <pre><code>.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;
  });
  </code></pre>
  
  <p>I have two questions regarding this: </p>
  
  <ol>
  <li><p>even if I get 401 status from backend I always end up in success function, So what I am doing wrong here.</p></li>
  <li><p>also If I remove <strong>return</strong> from $http.get then the functionality breaks. So What is the need for this return keyword in front of $http.get</p></li>
  </ol>
  
  <p>controller</p>
  
  <pre><code>dataSvc.list({url: '/api/users'}).then(function(result) {
      console.log('result here===========', result);
      if(result.status == 401) {
  
      }
      else {
  
      }
  
  });
  </code></pre>
  
  <p>and also what is the difference between then method and success and error methods provided by angular ?</p>