Skip to content

Auth promises and callbacks #456

@kingcody

Description

@kingcody

Looking over client/components/auth/auth.service it seems that it would be beneficial for us to use a signature for the methods that would allow for synchronous and async callback/promises.

I would propose:

function(callback, safe) {
  if (arguments.length === 0) {
    return synchronous.result;
  } else {
    var deferred = $q.defer();
    async.then(function(data) {
      deferred.resolve(data);
      callback(data);
    }, function() {
      (safe) ? deferred.resolve(null) : deferred.reject();
      callback(null);
    });
    return deferred.promise;
  }
}

Its not terribly complicated and I feel provides maximum usability. Such as, all current calls to the auth methods would be maintained sync, we could remove methods such as isLoggedInAsync, and also we could use the promises they return in resolves for routes which could seriously improve the routing authentication.

I'd also like to point out the use of the 2nd argument safe, I've used it to allow us to receive promises that only resolve and not reject. This could be useful in a route resolve as well, being that if a promise is rejected then $routeChangeError or $stateChangeError is fired and we may not always want that.

Another advantage would be that you could chain the promises with finer grained control, dictating whether or not a promise resolves into another (even on error) or is rejected and the chain canceled.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions