无极API - 2相结合的异步调用的结果 [英] Promise API - combining results of 2 asynchronous call

查看:158
本文介绍了无极API - 2相结合的异步调用的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用API​​的承诺,如何发送两个并行异步请求,并解决的综合结果作为响应。

With promise API, how to send two asynchronous request in parallel, and resolve the combined result as the response.

var get = function(id){
            var res1, res2;
            var deferred = $q.defer();
            Db.get(id, "abc")
                .then(function (d) {
                    //deferred.resolve(d));
                    res1 = d;
                }, function (e) {
                    //error
                });

            Db.get(id, "def")
                .then(function (d) {
                    //deferred.resolve(d));
                    res2 = d;
                }, function (e) {
                    //error
                });

            //?????? how to return {res1:res1 , res2: res2}

            return deferred.promise;
        };

现在,当我调用get(),如

now, when I call get() like

get(123).then(function(d)){
// d= {res1: res1, res2: res2}
},
...

我需要得到的综合结果所示。如何用角承诺API这样做呢?

I need to get the combined result as indicated. How to do this with Angular promise API?

推荐答案

作为@马特说,你需要使用 $ q.all ,但使用率不完全正确。 AngularJS不支持 .done .fail 和他们不太一样,反正在没有工作用作多个值承诺这样的事情,而不是你只是有一个数组的承诺。

As @Matt said, you need to use $q.all, but the usage isn't quite right. AngularJS doesn't support .done and .fail and they don't work quite like that anyway in that there's no such thing as a promise for multiple values, instead you just have a promise for an array.

如果您正在使用完整的 Q 我们会写这写:

If you were writing this using the full Q we would write:

var get = function (id) {
    return Q.all([Db.get(id, "abc"), Db.get(id, "def")])
        .spread(function (res1, res2) {
            return {res1: res1, res2: res2};
        });//the error case is handled automatically
};

在这种情况下, .S $ P $垫就像。然后除了它那$ P $垫数组的结果在其 onFulfilled 函数的参数的承诺了​​。要改变这种使用来自我们只需要凑合AngularJS的承诺方法,而 .S $ P $垫。这导致了下列溶液:

In this case, .spread acts like .then except that it spreads the results of an array for a promise out over the arguments of its onFulfilled function. To change this to use the promise methods from AngularJS we just need to make do without .spread. This leads to the following solution:

var get = function (id) {
    return $q.all([Db.get(id, "abc"), Db.get(id, "def")])
        .then(function (res) {
            return {res1: res[0], res2: res[1]};
        });//the error case is handled automatically
};

这样做的好处是,我们从处理错误传播的所有一些本质grity和存储部分的结果,因为。然后作为过滤器释放。如果你离开了错误处理程序,它会自动传播任何错误。这意味着,如果任一输入承诺被拒绝,结果将被拒绝。如果这两个承诺都成功完成,资源是那些分辨率值的数组。

The beauty of this is that we are freed from handling all the nitty grity of error propagation and storing the partial results because .then acts as a filter. If you leave out the error handler, it automatically propagates any errors. This means that if either of the input promises are rejected, the result will be rejected. If both promises are fulfilled successfully, res is the array of those resolution values.

这篇关于无极API - 2相结合的异步调用的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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