In Angular.js I had a controller where I had to execute an action after multiple service calls are completed. Thanks to the $q promise library integrated in Angular.js, this is easy to achieve:
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
(function (app) { | |
var step1Controller = function ($scope,applicationsService, environmentsService,$q) { | |
$scope.name = "Step 1 - Specify application and environment"; | |
$q.all([applicationsService.getApplications(),environmentsService.getEnvironments()]) | |
.then(function (responses) { | |
$scope.applications = responses[0].data; | |
$scope.environments = responses[1].data; | |
}).catch(function (data) { | |
$scope.applications = null; | |
$scope.environments = null; | |
})['finally'](function(){ | |
$scope.isBusy=false; | |
}); | |
}; | |
step1Controller.$inject = ["$scope", "applicationsService", "environmentsService","$q"]; | |
app.controller("step1Controller", step1Controller); | |
}(angular.module("authorizationScopeWizardApp"))); |
Both getApplications() and getEnvironments() return a Promise. By wrapping them in a call to $q.all(), the parent Promise is only resolved after both wrapped Promised are completed.