AngularJs承诺会在失败后执行

时间:2019-01-30 16:25:25

标签: angularjs promise

我有以下代码:

CaseFolderResource
 .exportByCaseId()
 .getWithGenericResponse(
    {caseId : request.caseId}, 
    function (result) {
      console.log(result);
      console.log('in 4')
    }, 
    ErrorHandler.handleError
  )
 .$promise
 .then(
   function (success) {
     console.log(success);
     console.log('in 2');
     console.log('success');

     CaseFolderResource
     .getByCaseId()
     .getWithGenericResponse(
       {caseId : request.caseId}, 
       function (result) {
         console.log('nested call');
         console.log(result);
       }
     );
   },  
   ErrorHandler.handleError
 );

console log中,我看到的是in,然后是in 2,然后是success,即使输出是失败的,所以Prolog记录: datasuccess:false

我在第一个和第二个承诺中获得HTTP 500。

编辑:

getWithGenericResponse是:

        getWithGenericResponse: {
            method: 'GET',
            interceptor: genericResponseInterceptor,
            headers: {
                'Authorization': function () {
                    return AuthHolder.getAuthData();
                },
                'X-Company': function(){
                    return getCompany();
                }
            }
        },

genericResponseInterceptor是:

var genericResponseInterceptor = {
    response: function (data) {
        var response = data.data;
        console.log(response);
        if (data.status === 401 || data.status === 403) {
            reject();
        } else if (!response.success && response.errors !== undefined && response.errors !== null) {
            response.errors.forEach(function (error) {
                console.log(error.code);
                if (error.message) {
                    ToasterService.showTranslatedWarningToasterInstant(error.message);
                } else {
                    ToasterService.showTranslatedWarningToaster('error.' + error.code, {params: error.args});
                }
            });
        }
        return response;
    },
    responseError: function (data) {
        if (data.status === 401) {
            reject();
        } else if (data.status === 403) {
            ToasterService.showTranslatedErrorToaster('error.message.403');
        } else {
            ToasterService.showTranslatedErrorToaster('error.message.default');
        }
        return data;
    }
};

即使我从第一个请求中删除:,ErrorHandler.handleError,结果也与上述相同。

编辑:

我可以用if (response.data.success)来解决它:

CaseFolderResource.exportByCaseId().getWithGenericResponse({caseId : request.caseId}).$promise
                    .then(function (response) {
                        console.log(response.data);
                        console.log(response.success);
                        if (response.data.success) {
                            console.log('in 2');
                            console.log('success');
                            CaseFolderResource.getByCaseId().getWithGenericResponse({caseId : request.caseId}, function (result) {
                                console.log('nested call');
                                console.log(result);
                            });
                        }
                        });

0 个答案:

没有答案