Hi James, The pattern you describe is called Promises <https://developer.mozilla.org/nl/docs/Web/JavaScript/Reference/Global_Objects/Promise>. They where in AngularJS before they arrived in JavaScript itself. They are a bit adapted in AngularJS to cater for the $digest loop. It's clearly stated that all $http calls return a promise.
Straight from the documentation: > The $http API is based on the deferred/promise APIs > <https://docs.angularjs.org/api/ng/service/$q> exposed by the $q service. > While for simple usage patterns this doesn't matter much, for advanced > usage it is important to familiarize yourself with these APIs and the > guarantees they provide. That's on top of the $http docs page. <https://docs.angularjs.org/api/ng/service/$http> Promises are a good pattern to use. In your case, I would move the 'unpacking' one to your service like this: $scope.login = function() { var loginResponse = {}; var errorMessage = ''; var access_token = ''; loginService .login(myInfo) .then(function(token) { $window.sessionStorage.setItem('token', token); access_token = token; }) .catch(function(err) { errorMessage = err.errorMessage.split(':').pop(); console.log('errorMessage is: ' + errorMessage); }); }; //service function loginService($window, $http, $httpParamSerializerJQLike) { var service = { login: login }; return service; function login(myInfo) { params = $httpParamSerializerJQLike(params); return $http({ url: baseURL, method: 'POST', data: params, //Set the headers so Angular is passing information as form data, not request payload. headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }).then(function onSuccess(response) { if (repsone.data.token) { return response.data.token; } throw new Error('no token in response'); }); } } Also I noticed you use $scope a lot. That's discouraged by the core team for a long time already. If you want to know why, read the style-guide <https://github.com/johnpapa/angular-styleguide> -- You received this message because you are subscribed to the Google Groups "Angular and AngularJS discussion" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/angular. For more options, visit https://groups.google.com/d/optout.
