Skip to content

Instantly share code, notes, and snippets.

@icfantv icfantv/AuthService.js
Last active Jan 6, 2016

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

wesleycho commented Jan 6, 2016

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
You can’t perform that action at this time.