AngularJS service retry when promise is rejected

后端 未结 5 2229
Happy的楠姐
Happy的楠姐 2020-12-13 11:06

I\'m getting data from an async service inside my controller like this:

myApp.controller(\'myController\', [\'$scope\', \'AsyncService\',
function($scope, As         


        
5条回答
  •  北荒
    北荒 (楼主)
    2020-12-13 11:16

    I ended up doing this a lot so I wrote a library to help address this problem : )

    https://www.npmjs.com/package/reattempt-promise-function

    In this example you could do something like

    myApp.controller('myController', ['$scope', 'AsyncService',
    function($scope, AsyncService) {
        var dogsQuery = { family: canine };
        $scope.online = true;
        $scope.getDogs = function() {
            return reattempt(AsyncService.query(dogsQuery)).then(function(dogs) {
                $scope.online = true;
                $scope.dogs = dogs;
            }).catch(function() {
                $scope.online = false;
            });
        }
    }]);
    

提交回复
热议问题