2

I have the following service in which there is the function to fetch the data for all dashboards of my system.

.service('dashboardServices', function ($http, $q) {
return {
    getData: getData
}

//definition of promises and their http requests urls
var promise01 = $http({method: 'GET', url: urlpromise01, cache: false});
var promise02 = $http({method: 'GET', url: urlpromise02, cache: false});
var promise03 = $http({method: 'GET', url: urlpromise03, cache: false});

function getData(){
    var promises = [];
        var promises = $q.all([promise01, promise02, promise03])
                            .then(function(data){
                                setDashboardData(data[0].data);
                                setDashboardData(data[1].data);
                                setDynamicDashboardData(data[2].data);
                                })
        return promises;
}
})

I am calling this service in my controller in order to open a modal window with loading message while fetching the data from the server.

.controller('MainCtrl', function($state, $scope, $ionicModal, dashboardServices) {
$ionicModal.fromTemplateUrl('views/loading.html', {
    scope: $scope,
    backdropClickToClose: false,
    hardwareBackButtonClose: true,
    animation: 'slide-in-up'
}).then(function(modal) {
    $scope.modal = modal;
});

$scope.openModal = function() {
    $scope.modal.show().then(function() {
        dashboardServices.getData().then(function() {
            $scope.closeModal();
        })
    })
}

$scope.closeModal = function() {
    $scope.modal.hide();
};
})

Now I want to have a mean to show some sort of message in case the requests failed. because right now when at least one of the request fails, the loading modal window will never get closed. but I can't add .error(function()... in my controller it will throw an error saying that .then().... .error() is not supported for $q.all

What would be a possible way to implement this exception handing?

Afflatus
  • 933
  • 1
  • 12
  • 39
  • Possible duplicate of [What happens with $q.all() when some calls work and others fail?](http://stackoverflow.com/questions/19944922/what-happens-with-q-all-when-some-calls-work-and-others-fail) – notAChance Jan 08 '16 at 16:50
  • you can also use `$q.all(promises).then(onSuccess).catch(function(error) { // handle error });` – martskins Jan 08 '16 at 16:53
  • indeed @lascort, I used the same method. – Afflatus Jan 09 '16 at 06:30

2 Answers2

4

.then(...).error(...) is not supported not only by $q.all but by $q promises and by promises in general. You probably confused it with 'error' callbacks, and the thing you're looking for is catch:

$q.all([...]).then(onSuccess).catch(onError);

Which may be an alternative form of

$q.all([...]).then(onSuccess, onError);

but suited better for readability. Or it may not be, if the there is a necessity to handle the errors from all(...) separately from the rest of chain, in this case it has to be

$q.all([...]).then(onAllSuccess, onAllError).catch(onError);
Estus Flask
  • 206,104
  • 70
  • 425
  • 565
  • it's more of a `$q.all([...]).then(onSuccess, undefined).then(undefined, onError);` , isn't it? – martskins Jan 08 '16 at 16:55
  • @lascort That's a good point. Usually there would be no difference, but the difference appears if the one wants to handle the errors from `all(...)` and `then(...)` separately. – Estus Flask Jan 08 '16 at 17:01
  • Thanks for the explanation! I used @whistling_marmot, I did try catch as well, it also works. – Afflatus Jan 09 '16 at 06:29
3
alertServices.getData().then(function() {
    $scope.closeModal();
}, function(reason) {
    // handle the error
});
whistling_marmot
  • 3,561
  • 3
  • 25
  • 39