Need help understanding how to handle promises


#1

I have a simple service to see if the fields in a lpgin form are empty

    .factory('Login', function($cordovaSQLite, $q, DBA) {

  var self = this;


  // VALIDATE LOGIN FORM VALUES
  self.validate = function(user){
    q = $q.defer();
    var valid = 0;
    if(user.username && user.password){
      q.resolve(1);
    }else{
      q.reject(0);
    }
    DBA.consoleWrite(q.promise);
    return q.promise;
  };
})

Then in my controller I have this to check it. I want to THEN do some other work based on this, but I am having a tough time getting it set up correctly.

The promise is returned, it’s an object that has the expected value if a form field is empty:

{"$$state":{"status":2}}

If the form is filled out completely I can see the promise object returned is different:

{"$$state":{“status”:1}}

But I can’t figure out how to use this in a THEN in my controller

.controller('LoginCtrl', function($scope, $state, $ionicPlatform, $cordovaSQLite, $ionicModal, $q, DBA, Login) {

  $scope.user = [];
  $scope.login = function(user) {
    DBA.consoleWrite(user);
    var valid = 0;
    var title = "";
    var msg = "";

    Login.validate(user)
      .then(function(result) {
        // I HAVE THE PROMISE HERE
      });
  }
})

If I want to add another THEN based on the result, is there a way to only do that THEN if the promise status is 1? If not, what is the correct way to handle a long chain of THEN functions? For instance, if I want to THEN look in a DB for this user if the fields aren’t empty, not if the DB is empty.