Created
April 22, 2014 13:28
-
-
Save alextucker/11179274 to your computer and use it in GitHub Desktop.
Promise Returning Service Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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