我正在尝试填充一些解决一系列远程调用的本地数据.
当每个承诺都得到解决后,我会加载数据并继续.
该方法$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)
输出:
[宾语]
有关如何将其包裹起来以保持弹性的任何暗示?
但是,一个令人讨厌的异常仍然显示在控制台上...如果我无法尝试/捕获异步请求,我怎么能摆脱它?
来电代码
return $q.all([
this.getUserInfo(user_id)
.then(function (r) …Run Code Online (Sandbox Code Playgroud)