Angular 如何处理由 $q.all() 执行的 $http.get 或 $http.jsonp 请求的不可用 URL [英] Angular how to deal with unavailable URLs requested by $http.get or $http.jsonp, which are executed by $q.all()

查看:23
本文介绍了Angular 如何处理由 $q.all() 执行的 $http.get 或 $http.jsonp 请求的不可用 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

eventResourcesCall = $http.jsonp('https://apicall/to/serverA');
eventsDetailsCall = $http.get('https://apicall/to/serverB');

$q.all([eventResourcesCall, eventsDetailsCall]).then(function(values){
    //process data manipulation and merging
});

问题是 serverA 和 ServerB 有时可能不可用,当其中一个不可用时,数据处理代码停止,我收到类似于下面描述的错误:

The problem is that serverA and ServerB might not be available sometimes, and when one of those are unavailable, the data processing code stops and I get an error similar to the one described below:

GET https://apicall/to/serverA?jsonp=angular.callbacks._0 404 (Not Found)

有人能指点我一份文档或在答案中描述如何正确处理 $http 请求并由 $q.all() 执行的不可用 URL 吗?

Can any one point me to a documentation or describe on the answer how to properly deal with unavailable URL requested by $http and executed by $q.all()?

我希望能够做的是获得 URL 不可访问的指示,然后避免数据处理代码中止.

What I would like to be able to do is to get an indication that the URL is not accessible and then avoid the data processing code abortion.

谢谢!

推荐答案

我会使用间接承诺:

var d1 = $q.defer(), d2 = $q.defer();

function NetworkError(reason) { this.reason = reason; }

eventResourcesCall = $http.jsonp('https://apicall/to/serverA').then(
    function(response) {
        d1.resolve(response);
    },
    function(err) {
        d1.resolve(new NetworkError(err));
    }
);
eventsDetailsCall = $http.get('https://apicall/to/serverB').then(
    function(response) {
        d2.resolve(response);
    },
    function(err) {
        d2.resolve(new NetworkError(err));
    }
);

$q.all([d1, d2]).then(function(values){
    var eventResources = values[0], eventsDetails = values[1];

    if( eventResources instanceof NetworkError ) {
        // handle error
    }
    else {
        // eventResources is good, use it
    }

    // and so on...
});

所以间接承诺总是得到解决并且 all() 成功.但是解析值可能是特殊的 NetworkError 类,它表示此请求中的实际错误.

So the indirect promises are allways resolved and the all() succeeds. But the resolution value may be of the special NetworkError class which signals the actual error in this request.

这绝对是笨重的,但可以通过一些实用方法来改进,例如:

This is definitely bulky, but could be improved with some utility methods, e.g.:

function makeIndirectPromise(httpPromise) {
    var ret = $q.defer();
    httpPromise.then(
        function(response) {
            ret.resolve(response);
        },
        function(err) {
            ret.resolve(new NetworkError(err));
        }
    );
    return ret.promise;
}

上面的代码变成了:

function NetworkError(reason) { this.reason = reason; }

function makeIndirectPromise(httpPromise) { /* see above */ }

eventResourcesCall = makeIndirectPromise($http.jsonp('https://apicall/to/serverA'));
eventsDetailsCall = makeIndirectPromise($http.get('https://apicall/to/serverB'));

$q.all([eventResourcesCall, eventsDetailsCall]).then(function(values){
    var eventResources = values[0], eventsDetails = values[1];

    if( eventResources instanceof NetworkError ) {
        // handle error
    }
    else {
        // eventResources is good, use it
    }

    // and so on...
});

这篇关于Angular 如何处理由 $q.all() 执行的 $http.get 或 $http.jsonp 请求的不可用 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆