5

Is it OK to do that ?

for example, I have my log out service

  logout: function() {
    var defer = $q.defer();

    this.getCustomer().then(function(credentials) {
      $http.post(CONSTANT_VARS.BACKEND_URL + '/auth/logout',
      {username: credentials.username, customer: credentials.customer}
    ).success(function(data) {
        if (data.error) {
          defer.reject(data);
        }
        LocalForageFactory.remove(CONSTANT_VARS.LOCALFORAGE_CUSTOMER).then(function() {
          /*Removing LocalForage Items*/
          cleanLocalForage();
          defer.resolve(data);
        }, function(err) {
          console.log(err);
          defer.reject(data);
        });
      }).error(function(data) {
        cleanLocalForage();
        defer.reject(data);
      });
    }, function(err) {
      defer.reject(err);
    });
    return defer.promise;
  },

and then I have a function in a controller which returns an error once the session expires. When the session expires I need to logout the user and redirect him to the login path. So, this is what I have so far:

$scope.removeSlip = function(slip) {
  BetSlipFactory.removeSlip(slip).then(function() {
  }, function(err) {
    console.log(err);
    AuthFactory.logout();
    $location.path('/');
  });
};

or should I do something like this with the logout promise into the BetSlipFactory.removeSlip() promise ?

$scope.removeSlip = function(slip) {
  BetSlipFactory.removeSlip(slip).then(function() {
  }, function(err) {
    console.log(err);
    AuthFactory.logout().then(function() {
     $location.path('/');
    })
  });
};

there is: what is the proper way in this case ?

Non
  • 8,409
  • 20
  • 71
  • 123
  • Sorry, I misread the question. I'd go with the latter – Phil May 06 '15 at 22:59
  • 6
    I'm voting to close this question as off-topic because it belongs on http://codereview.stackexchange.com/ – Phil May 06 '15 at 22:59
  • 2
    Clearly, your `AuthFactory.logout` is async. If you care about waiting for a logout to happen before you redirect (which makes sense), then you should use the second approach. Otherwise `$location.path` will happen prior to the logout process finishing... – New Dev May 06 '15 at 23:01
  • @Phil why are you doing that ? wait until someone with arguments answer it. Yizus – Non May 06 '15 at 23:01
  • 2
    @NietzscheProgrammer don't worry, it's not a bad thing. The folks over at code-review are all about *"what is the best option..."*. You don't appear to have a problem with your code, just different ways to accomplish the same thing – Phil May 06 '15 at 23:03
  • @Phil should I go and post my question there ? or the question will be transferred or ... ? – Non May 06 '15 at 23:04
  • It should be transferred if enough people vote to close for the same reason – Phil May 06 '15 at 23:04
  • [No, it is not](http://stackoverflow.com/q/23803743/1048572) – Bergi May 07 '15 at 00:30
  • Side note: Your `logout()` method is using the [deferred antipattern](http://stackoverflow.com/questions/23803743/what-is-the-deferred-antipattern-and-how-do-i-avoid-it). – JLRishe May 07 '15 at 06:45

0 Answers0