Last active
August 29, 2015 14:10
-
-
Save chazcheadle/bbf27afd373df3b0c71c to your computer and use it in GitHub Desktop.
AngularJS - Promise
This file contains hidden or 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
| /** | |
| * This is a factory promise that uses $http | |
| */ | |
| .factory('myFactory', ['$http', '$q', function($http, $q) { | |
| var getData = function() { | |
| var deferred = $q.defer(); | |
| $http({method:"GET", url:"/index.php", cache: false}) | |
| .success(function(data, status, headers, config) { | |
| date = formatDate(headers('Date'), 4); | |
| deferred.resolve(date); | |
| }) | |
| .error(function(data, status, headers, config) { | |
| }); | |
| return deferred.promise; | |
| }; | |
| return { getData: getData }; | |
| }]); | |
| /** | |
| * Use the promise | |
| */ | |
| myFactory.getData.then(function(date) { | |
| // do seomthing with 'date' once it is available. | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment