相关疑难解决方法(0)

AngularJS - $ q.all()失败

我正在尝试填充一些解决一系列远程调用的本地数据.
当每个承诺都得到解决后,我会加载数据并继续.

该方法$q.all( [] )正是如此:

        $q.all([
            this.getUserInfo(11)
                .then(function (r) {
                    results.push(r)
                }),

            this.getUserConns()
                .then(function (r) {
                    results.push(r)
                }),

            this.getUserCtxs()
                .then(function (r) {
                    results.push(r)
                })
        ])
        .then(function () {
            console.log(results)
        })
Run Code Online (Sandbox Code Playgroud)


问题是,这段代码没有弹性.
如果这些呼叫中的任何一个失败,没有人会得到鱼!

在try/catch语句中包装调用只会导致$q.all()完全忽略该条目,即使没有失败(请注意func中的console.log)...

        $q.all([
            this.getUserInfo2(11)
                .then(function (r) {
                    results.push(r)
                }),

            function () {
                try {
                    this.getUserGroups()
                        .then(function (r) {
                            console.log(r)
                            results.push(r)
                        })
                }
                catch (err) {
                    console.log(err)
                }
            },
        ])
        .then(function () {
            console.log(results)
        })
Run Code Online (Sandbox Code Playgroud)

输出:

[宾语]


有关如何将其包裹起来以保持弹性的任何暗示?


感谢@dtabuenc,我更进了一步.实现错误回调,我可以避免链断开,并推送已解决的promises的值.

但是,一个令人讨厌的异常仍然显示在控制台上...如果我无法尝试/捕获异步请求,我怎么能摆脱它?

来电代码

    return $q.all([

            this.getUserInfo(user_id)
                .then(function (r) …
Run Code Online (Sandbox Code Playgroud)

ajax promise angularjs q

17
推荐指数
1
解决办法
1万
查看次数

标签 统计

ajax ×1

angularjs ×1

promise ×1

q ×1