Promise API - combining results of 2 asynchronous call

前端 未结 3 865
情话喂你
情话喂你 2020-12-09 03:57

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

var get = function(id){
            var         


        
3条回答
  •  轮回少年
    2020-12-09 04:38

    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.

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

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

    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.

提交回复
热议问题