Skip to content

Instantly share code, notes, and snippets.

@codiodes
Last active November 1, 2017 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codiodes/193b7b2188f3483834a313307b8ce15d to your computer and use it in GitHub Desktop.
Save codiodes/193b7b2188f3483834a313307b8ce15d to your computer and use it in GitHub Desktop.
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