Skip to content

Instantly share code, notes, and snippets.

@icfantv
Last active January 6, 2016 20:37
Show Gist options
  • Save icfantv/e6db5bcd8a9a5520ff78 to your computer and use it in GitHub Desktop.
Save icfantv/e6db5bcd8a9a5520ff78 to your computer and use it in GitHub Desktop.
AuthService with promise and cache
angular.service('AuthService', function($q, $http) {
var authStuff, promise;
this.login = function(credentials) {
if (!authStuff) {
promise = $http.post(...).then(function(response) { authStuff = response.data; });
}
return $q(function(resolve, reject) {
promise.then(resolve(...)).catch(reject(...));
});
}
})
@wesleycho
Copy link

angular.service('AuthService', function($q, $http) {

  var authStuff;
  this.login = function(credentials) {
    var promise;
    if (!authStuff) {
      promise = $http.post(...).finally(response => authStuff = response);
    }

    return authStuff ? $q.resolve(authStuff) : promise;
  }
})

Perhaps?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment