简体   繁体   中英

Angular promises in $q.all are not being rejected

Background

I have a hierarchy of entities that looks like (Manufacturer -> Vehicle Type -> Vehicle Model -> Vehicle Submodel) . Each manufacturer has multiple vehicle types, each vehicle type has multiple models, and each model has multiple submodels.

I need to retrieve a list of vehicle models given a manufacturer and type, and then for each of those models, retrieve all of its submodels ( getSubmodels ). Once they're all fetched I place them into a JSON object. I'm trying to use Angular's $q module to ensure that all submodels have been retrieved before I continue execution - aka I need all of the promises to be resolved before I can move on and render the page components.

Issue:

The 'happy' path works perfectly, but if one of the requests for submodels has an error, the rejected promise from the submodel function does not get caught by the $q.all().then block, and thus the overall getVehicleHierarchy promise does not get rejected.

angular.module('uiApp.services.hierarchy', ['restangular'])
    .service('VehicleHierarchy', function VehicleHierarchy($http, $q, Restangular) {
        this.getVehicleHierarchy = function (manufacturerId, vehicleTypeId, vehicleModelId) {
            var that = this;
            var deferred = $q.defer();
            var promise = deferred.promise;
            var promises = [];
            Restangular.configuration.baseUrl = urlBuilder.buildHierarchyServiceUrl() + '/vehicle-hierarchy';
            Restangular.one('manufacturer', manufacturerId).one('type', vehicleTypeId).one('class', vehicleClassId).customGET('models')
                .then(function (models) {
                    var result = {};
                    _.forEach(models.result, function (model) {
                        result[parseInt(model.id)] = model;
                    });
                    _.forEach(result, function (model) {
                        promises.push(that.getSubmodels(manufacturerId, vehicleTypeId, vehicleClassId, model.id));
                    });
                    $q.all(promises).then(function (results) {
                        var i = 0;
                        _.forEach(result, function (model) {
                            result[parseInt(model.id)].subModels = results[i++];
                        }, function (errors) {
                            deferred.reject(errorResponse);
                        });
                        deferred.resolve(result);
                    });
                }, function(error) {
                    deferred.reject('error!');
                });
            return promise;
        };

        this.getSubmodels = function (manufacturerId, vehicleTypeId, vehicleClassId, modelId) {
            var submodels = {};
            var deferred = $q.defer();
            Restangular.configuration.baseUrl = urlBuilder.buildHierarchyServiceUrl() + '/vehicle-hierarchy';
            Restangular.one('manufacturer', brandId).one('type', vehicleTypeId).one('class', vehicleClassId).one('model', modelId)
                .customGET('submodels').then(function (submodelResponse) {
                    _.forEach(subclassResponse.result, function (subModel) {
                        subclasses[parseInt(subModel.id)] = subModel;
                    });
                    deferred.resolve(subclasses);
                }, function (error) {
                    deferred.reject('error'!);
                });
            return deferred.promise;
        };
    });
});

您需要返回$q.all呼吁您的误差函数中then抓住它:

return $q.all(promises).then(function (results) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM