q.all 不适用于多个承诺 [英] q.all not working for multiple promises

查看:17
本文介绍了q.all 不适用于多个承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 q.all 调用来解决两个承诺.我检查了所有帖子并尝试了所有其他实现方式 q.all 和相同的情况

I have the following q.all calling to resolve two promises. I checked all the posts and tried all other ways of implementation q.all and its the same case

var xyzdeffered = $q.defer();
service1.getServiceDetail1($routeParams.id).then(function(promise) {
    xyzdeffered.resolve(promise);
});

var abcdeffered = $q.defer();
service2.getServiceDetail2($routeParams.id).then(function(promise) {
abcdeffered.resolve(promise);
});


$q.all([ xyzdeffered, abcdeffered ]).then(function(data) {

    $scope.variable = data;

});

我期望 q.all 中的变量只有在前两个承诺得到解决后才会被填充.但不幸的是,服务调用本身并没有返回数据,并且控制转移到 q.all.我觉得这很奇怪,因为根据文档,只有当您的承诺返回 200 个响应并得到解决时,才会调用 q.all.我检查了分析网络调用并放置了一些警报以查看代码的顺序,发现 q.all 警报是第一个弹出的警报,然后解决了其他承诺.这真的让我很生气,为什么 q.all 的简单实现不起作用..任何帮助将不胜感激.

I am expecting the variable in q.all should get populated only after the first two promises are resolved. But unfortunately the service call itself doesn't get returned with data and the control moves on to the q.all. I find this weird because as per documentation the q.all is called only when your promises are returned with 200 response and are resolved. I checked analysing the network calls and also put some alert to see the sequence of the code and find that the q.all alert is the first alert to be popped up and then the other promises are resolved. Its really making me mad as why a simple implementation of q.all doesnt work.. Any help will be appreciated.

推荐答案

为什么不直接调用 $q.all on first promise 呢?

Why not directly call $q.all on first promise ?

$q.all([
    service1.getServiceDetail1($routeParams.id),
    service2.getServiceDetail2($routeParams.id)
]).then(function(data) {
    //Array of result [resultOfgetServiceDetails1, resultOfgetServiceDetails2]
    $scope.variable = data;
});

这篇关于q.all 不适用于多个承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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