Skip to content

Instantly share code, notes, and snippets.

@toddmotto
Created May 28, 2014 09:34
Show Gist options
  • Save toddmotto/6593de8f2f25ad43543a to your computer and use it in GitHub Desktop.
Save toddmotto/6593de8f2f25ad43543a to your computer and use it in GitHub Desktop.
Using $q.defer() with $http in Angular to return the Promise Object only
var query = function (endpoint) {
var deferred = $q.defer();
$http.get(endpoint)
.success(function (data) {
deferred.resolve(data);
})
.error(function (data) {
deferred.reject(data);
});
return deferred.promise;
};
// usage
var someGet = query('/someEndpoint');
someGet.then(function (data) {
// success, do something with data
// typically bind to a Controller `this` value
}, function (reason) {
// fail, do something with reason
});
@StefanFeser
Copy link

This is something I use almost every day. Never thought about making a gist :D Thanks!

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