Skip to content

Instantly share code, notes, and snippets.

@icfantv
Last active September 10, 2015 17:10
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 icfantv/9ad2101e7fbb4e5cedea to your computer and use it in GitHub Desktop.
Save icfantv/9ad2101e7fbb4e5cedea to your computer and use it in GitHub Desktop.
Promises Example
var app = angular.module('app', []);
app.service('MyHttpService', ['$http', MyHttpService]);
function MyHttpService($http) {
this.doHttpCall = function() {
return $http.get('/foo.json');
};
}
app.service('MyService', ['$q', MyService]);
function MyService($q) {
this.doSomething = function() {
return $q(function(resolve, reject) {
resolve('hi mom!');
});
};
}
app.controller('MyController', ['$q', 'MyHttpService', 'MyService', MyController]);
function MyController($q, MyHttpService, MyService) {
$q.all([MyHttpService.doHttpCall(), MyService.doSomething()]).then(function(myHttpServiceResult, myServiceResult) {
console.log(myHttpServiceResult.name); // steve
console.log(myServiceResult); // hi mom!
});
}
{
name: 'steve'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment