How to stop/break in the middle of chained promises

大城市里の小女人 提交于 2019-12-01 09:23:30
bmceldowney

From the $q.reject documentation:

When comparing deferreds/promises to the familiar behavior of try/catch/throw, think of reject as the throw keyword in JavaScript. This also means that if you "catch" an error via a promise error callback and you want to forward the error to the promise derived from the current promise, you have to "rethrow" the error by returning a rejection constructed via reject.

reject won't automatically abort the promise chain, it will just continue to the next promise, calling the error handler for each promise remaining.

Also, the finally callback is always going to run regardless of whether the chain is errored or not. If you don't want it to run then it's up to you to check the status of the promise manually.

EDIT:

Here's a link to an answer that shows how to chain errors:

Break promise chain and call a function based on the step in the chain where it is broken (rejected)

Endy Tjahjono

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"); }
        );
    }
]);
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<div ng-app="MyModule" ng-controller="MyCtrl">
  <p>result:</p>
  <div ng-repeat="msg in result">{{msg}}</div>
</div>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!