Skip to content

Instantly share code, notes, and snippets.

@alextucker
Created April 22, 2014 13:28
Show Gist options
  • Save alextucker/11179274 to your computer and use it in GitHub Desktop.
Save alextucker/11179274 to your computer and use it in GitHub Desktop.
Promise Returning Service Pattern
angular.module('myapp.services.usersettings', [])
.factory('UserSettingsService', ['$q', '$http', function($q, $http){
return {
getSettings: function(id) {
var defer = $q.defer(); // A defer object that you control
$http.get('some/url')
.success(function(resp){
defer.resolve(data);
})
.error(function(){
defer.reject(data);
});
return defer.promise; // Return the promise of the defer you control
}
}
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment