Last active
September 10, 2015 17:10
-
-
Save icfantv/9ad2101e7fbb4e5cedea to your computer and use it in GitHub Desktop.
Promises Example
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
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! | |
}); | |
} |
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
{ | |
name: 'steve' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment