angularjs返回被拒绝的承诺

r3plica

我在这里有一些奇怪的行为....我有一个创建来处理所有API调用的服务。看起来像这样:

angular.module('widget.data').service('apiHandler', apiHandler);

apiHandler.$inject = ['$http', 'SimpleCache', 'apiUrl', 'ngNotify'];

function apiHandler($http, simpleCache, apiUrl, ngNotify) {

    return {
        url: apiUrl,

        get: get,
        post: post,
        put: put,
        delete: requestDelete
    };

    //////////////////////////////////////////////////

    // GET
    function get(url, params) {
        return buildRequest(url, 'GET', null, params);
    };

    // POST
    function post(url, data) {
        return buildRequest(url, 'POST', data);
    };

    // PUT
    function put(url, data) {
        return buildRequest(url, 'PUT', data);
    };

    // DELETE
    function requestDelete(url, params) {
        return buildRequest(url, 'DELETE', null, params);
    };

    // Private function to build our request
    function buildRequest(url, method, data, params) {

        //console.log(url);

        // Create our apiPath
        var apiPath = url.indexOf('http') > -1 ? url : apiUrl + url;

        // Create the model
        var model = {
            method: method,
            url: apiPath,
            data: data,
            params: params,
            cache: simpleCache
        };

        // If we are performing an update/create or delete call
        if (method !== 'GET') {

            // Remove from our cache
            simpleCache.remove(apiUrl + url);
        }

        // Build our request
        return $http(model).then(function (response) {

            // Return our response
            return response.data;

            // If we have an error
        }, function (response) {

            console.log(response.data.exceptionMessage);

            // Display our error
            ngNotify.set(response.data.exceptionMessage, { type: 'error' });

            // Return our error
            return response;
        });
    };
};

您可以在buildMessage中看到它返回调用,响应和错误响应。因此,我希望如果发生错误,注入此错误的任何服务也会触发错误回调。但事实并非如此。我有这项服务:

angular.module('widget.directives').service('pkAuthenticateService', pkAuthenticateService);

pkAuthenticateService.$inject = ['apiHandler'];

function pkAuthenticateService(apiHandler) {
    return {
        register: register
    };

    //////////////////////////////////////////////////

    function register(model) {

        return apiHandler.post('users/create', model).then(function (response) {
            console.log('success', response);
            return response;
        }, function (response) {
            console.log('error', response);
            return response;
        });
    };
};

但是我在控制台中收到的消息是成功消息,而不是错误。有人可以向我解释原因吗?或者帮助我使其按预期运行(即,如果它在父服务中失败,那么它应该一直失败)。

MichałSałaciński

这是JS承诺中最有趣和最令人困惑的案例之一-“吞噬”错误。考虑以下情况:

var x = new Promise((result,reject)=>{
  reject('something bad')
})

x.then(()=>{
  console.log('wow')
  return 'a';
},()=>{
  console.log('ops')
  return 'a';
}).then(()=>{
  console.log('I\'m baaack')
  return 'a';
},()=>{
  console.log('still bad?')
  return 'a';
})

这可能是违反直觉的,但是在第二nd then()内部将打印“ I \'m baaack”,因为您已经捕获并处理了错误。因此,如果要处理错误(使用then()的第2个参数或带有一些“捕获”),则需要抛出错误或返回被拒绝的内容以进一步传递错误。

在您的情况下,可以不用$ q来完成,只需替换1行:

        // If we have an error
    }, function (response) {

        console.log(response.data.exceptionMessage);

        // Display our error
        ngNotify.set(response.data.exceptionMessage, { type: 'error' });

        // Return our error
        throw response;
    });

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章