Created
May 28, 2014 09:34
-
-
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
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
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 | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is something I use almost every day. Never thought about making a gist :D Thanks!