Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AngularJS Promises Use
//controller
angular.module('app.controllers')
.controller('GitCtrl', function(GitApi) {
GitApi.fetchUser('codiodes')
.then(function(user) {
console.log(user);
}, function(error) {
console.log(error);
})
})
//factory
angular.module('app.factories')
.factory('GitApi', function($http, $q) {
return {
fetchUser: function(username) {
var url = 'https://api.github.com/users/' + username;
var promise = $q.defer();
$http.get(url)
.success(function (data, status, headers, config) {
defer.resolve(data);
})
.error(function (data, status, headers, config) {
defer.reject(data);
})
return promise;
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.