Last active
November 1, 2017 19:56
-
-
Save codiodes/193b7b2188f3483834a313307b8ce15d to your computer and use it in GitHub Desktop.
AngularJS Promises Use
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
//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