I have a chain of $http calls to server. If one call fails I want to display notification to user and stop the chain. At first I thought I can use the $q.reject to stop the
This is my conclusion after reading the link provided by @bmceldowney:
The flow will always go to the next then
, so in order to stop, don't provide the next then
on the path / promise that needs to stop, and put the next then
only on the path / promise that needs to continue.
In my case I don't want the chain to continue after receiving error on the first promise, so the next then
should be appended on the second promise, not on the first then
:
d0.promise.then(
function(response) {
// ...
return d1.promise.then( // append the next then here ...
// ...
).catch (
// ...
).finally(
// ...
);
}
).catch (
// ...
).finally(
// ...
); // ... instead of here
angular.module("MyModule", []).controller("MyCtrl", ["$scope", "$q", "$timeout",
function($scope, $q, $timeout) {
$scope.result = [];
var d0 = $q.defer();
$timeout(function() { d0.reject(); }, 1000);
d0.promise.then(
function(response) {
$scope.result.push("d0 successful");
var d1 = $q.defer();
$timeout(function() { d1.reject(); }, 1000);
return d1.promise.then(
function(response) { $scope.result.push("d1 successful"); }
).catch (
function(response) { $scope.result.push("d1 failed"); }
).finally(
function() { $scope.result.push("finally2"); }
);
}
).catch (
function(response) { $scope.result.push("d0 failed"); }
).finally(
function() { $scope.result.push("finally1"); }
);
}
]);
result:
{{msg}}