Skip to content

Instantly share code, notes, and snippets.

@philipmadeley
Last active September 16, 2015 01:59
Show Gist options
  • Save philipmadeley/4ee366d79778ba8d4947 to your computer and use it in GitHub Desktop.
Save philipmadeley/4ee366d79778ba8d4947 to your computer and use it in GitHub Desktop.
Angular service with promise
app.factory('SonService', function($http, $q) {
return {
getWeather: function() {
// the $http API is based on the deferred/promise APIs exposed by the $q service
// so it returns a promise for us by default
return $http.get('http://fishing-weather-api.com/sunday/afternoon')
.then(function(response) {
if (typeof response.data === 'object') {
return response.data;
} else {
// invalid response
return $q.reject(response.data);
}
}, function(response) {
// something went wrong
return $q.reject(response.data);
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment