Skip to content

Instantly share code, notes, and snippets.

@datakop
Created August 15, 2014 08:54
Show Gist options
  • Save datakop/01849c1b2df0d96d556c to your computer and use it in GitHub Desktop.
Save datakop/01849c1b2df0d96d556c to your computer and use it in GitHub Desktop.
Resolve complicated data for controller. AngularJS.
currentNgModule.factory("Service1", function($log, $q, $timeout){
return function() {
var deferred = $q.defer();
$timeout(function(){
$log.debug("In Service1");
deferred.resolve("Service1");
},1000);
return deferred.promise;
}
});
currentNgModule.factory("Service2", function($log, $q, $timeout){
return function() {
var deferred = $q.defer();
$timeout(function(){
$log.debug("In Service2");
deferred.resolve("Service2");
},2000);
return deferred.promise;
}
});
currentNgModule.factory("MiltiService", function($log, $q, Service1, Service2) {
return $q.all([Service1(),Service2()]);
});
currentNgModule.factory("ControllerResolver", function($log, $q, MiltiService, Service1, Service2) {
return $q.all([
MiltiService,
Service1() // Chain unit 1
.then(function (data) {
$log.debug("In Service1 then", data);
return Service2(); // return promise // Chain unit 2
}).then(function (data) {
$log.debug("In Service2 then", data);
return Service1(); // return promise // Chain unit 2
}),
Service2().then(function (data) {
$log.debug("In Service2 2 then", data);
return data; // Return Data into q.all the function
})
]).then(function (data) {
$log.debug("In q.all then", data)
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment