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

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

问题描述

使用promise 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}
},
...

我需要获得所示的组合结果.如何使用 Angular promise API 做到这一点?

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

推荐答案

正如@Matt 所说,需要使用$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
};

在这种情况下,.spread 的作用类似于 .then,不同之处在于它将承诺的数组结果传播到其 onFulfilled<的参数上/代码> 功能.要将其更改为使用 AngularJS 的 promise 方法,我们只需要不使用 .spread 即可.这导致了以下解决方案:

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
};

这样做的好处在于,我们无需处理错误传播的所有细节,也无需存储部分结果,因为 .then 充当过滤器.如果您省略错误处理程序,它会自动传播任何错误.这意味着如果任何一个输入承诺被拒绝,结果将被拒绝.如果两个承诺都成功实现, res 是这些分辨率值的数组.

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.

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

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