Skip to content

Instantly share code, notes, and snippets.

@chazcheadle
Last active August 29, 2015 14:10
Show Gist options
  • Select an option

  • Save chazcheadle/bbf27afd373df3b0c71c to your computer and use it in GitHub Desktop.

Select an option

Save chazcheadle/bbf27afd373df3b0c71c to your computer and use it in GitHub Desktop.
AngularJS - Promise
/**
* 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