Reputation: 6213
I have the following function in an Angular.js controller:
vm.some_action = function() {
var promise = IDService.getUserInfo().then(function(user) {
vm.user.name = user.name;
vm.box.color = 'red';
vm.save()
}, function(errorResponse) {
Toast.error("Failed:", errorResponse);
})
$q.resolve(promise).then(function() {
$location.path('/home');
})
}
It should wait for all function calls within the promise to finish and only then redirect to /home
. However, it seems to execute all three (two assignments and a call to save()
) and redirect right away. What's the magic way to tell it to wait for the save()
function to finish as well?
Upvotes: 0
Views: 347
Reputation: 13346
Chain your promises like below:
vm.some_action = function() {
IDService.getUserInfo().then(function(user) {
vm.user.name = user.name;
vm.box.color = 'red';
return vm.save()
}).then(function() {
$location.path('/home');
}, function(errorResponse) {
Toast.error("Failed:", errorResponse);
});
}
Upvotes: 1