我试图填补一些本地数据解决了一系列的远程调用。 当每一个承诺解决后,我加载数据,然后继续。
方法 $ q.all([])
完全做到这一点:
$ q.all([
this.getUserInfo(11)
。然后(函数(R){
results.push(r)的
}),
this.getUserConns()
。然后(函数(R){
results.push(r)的
}),
this.getUserCtxs()
。然后(函数(R){
results.push(r)的
})
])
。然后(函数(){
执行console.log(结果)
})
问题是,这个code不是弹性的。照片 如果这些呼叫失败,没有人得到的鱼!
结束语调用在一个try / catch语句,只是使 $ q.all()
完全忽略条目,即使没有失败(请注意的console.log在FUNC)...
$ q.all([
this.getUserInfo2(11)
。然后(函数(R){
results.push(r)的
}),
功能 () {
尝试 {
this.getUserGroups()
。然后(函数(R){
执行console.log(R)
results.push(r)的
})
}
赶上(错误){
执行console.log(ERR)
}
},
])
。然后(函数(){
执行console.log(结果)
})
输出:
[对象]
我如何可以换这个任何暗示是有弹性? 由于@dtabuenc,我已经走了一步。 实施错误回调,我可以避开链的断裂,并推动解决了承诺的价值。
然而,一个讨厌的异常仍显示在控制台上... 我该如何摆脱,如果我不能够在异步请求的try / catch?
来电code
返回$ q.all([
this.getUserInfo(USER_ID)
。然后(函数(R){
结果['personal_details'] = R
}),
this.getUserConns()
。然后(
函数(r)的{
结果['连接'] = R
},
功能(错误){
执行console.log(ERR)
})
])
。然后(函数(){
返回(结果)
})
被叫code(带异常注)
getUserConns:函数(){
返回__doCall(ws.getUserConnections,{})
。然后(函数(R){
//非常通用的异常注入
抛出新的错误
如果(R&安培;&放大器; r.data ['return_ code'] === 0){
返回r.data ['项']
}
其他 {
的console.log(无法检索活动 - 错误:'+ r.data ['return_ code'])
返回NULL
}
})
},
解决方案
这将工作,但也推动了错误的阵列。
函数推(R){
results.push(r)的;
}
$ q.all([
this.getUserInfo(11)。然后(推).catch(推)
this.getUserConns()。然后,(推).catch(推)
this.getUserCtxs()。然后,(推).catch(推)
])
。然后(函数(){
执行console.log(结果);
})
您应该还可以提高你的承诺的理解,您从来没有应使用的try-catch
与承诺 - 使用的承诺时,您可以使用 .catch()
办法(与其他一切隐含一个尝试
)。这适用于正常的错误和异步错误。
如果你想完全忽视了错误:
函数推(R){
results.push(r)的;
}
功能空操作(){}
$ q.all([
this.getUserInfo(11)。然后(推).catch(空操作),
this.getUserConns()。然后,(推).catch(空操作),
this.getUserCtxs()。然后,(推).catch(空操作)
])
。然后(函数(){
执行console.log(结果);
})
I'm trying to fill some local data resolving a series of remote calls. When every promise is resolved, I load the data and proceed.
The method $q.all( [] )
does exactly this:
$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)
})
Problem is, this code is not resilient. If any of these call fails, nobody gets the fish!
Wrapping the calls in a try/catch statement, simply causes $q.all()
to entirely ignore the entry, even when not failing (note the console.log in the func)...
$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)
})
Output:
[Object]
Any hint on how I could wrap this to be resilient? Thanks to @dtabuenc, I've gone one step further. Implementing the error callback, I can avoid the breaking of the chain, and push the values of the resolved promises.
However, a nasty Exception is still displayed on the console... How can I get rid of that if I cannot try/catch on async requests?
Caller code
return $q.all([
this.getUserInfo(user_id)
.then(function (r) {
results['personal_details'] = r
}),
this.getUserConns()
.then(
function (r) {
results['connections'] = r
},
function(err) {
console.log(err)
})
])
.then(function () {
return (results)
})
Callee code (inject with an exception)
getUserConns: function() {
return __doCall( ws.getUserConnections, {} )
.then( function(r) {
// very generic exception injected
throw new Error
if (r && r.data['return_code'] === 0) {
return r.data['entries']
}
else {
console.log('unable to retrieve the activity - err: '+r.data['return_code'])
return null
}
})
},
解决方案
This will work but also push the errors to the array.
function push(r) {
results.push(r);
}
$q.all([
this.getUserInfo(11).then(push).catch(push),
this.getUserConns().then(push).catch(push),
this.getUserCtxs().then(push).catch(push)
])
.then(function () {
console.log(results);
})
You should also improve your understanding of promises, you never should use try-catch
with promises - when using promises, you use the .catch()
method (with everything else being implicitly a try
). This works for normal errors as well as asynchronous errors.
If you want to totally ignore the errors:
function push(r) {
results.push(r);
}
function noop() {}
$q.all([
this.getUserInfo(11).then(push).catch(noop),
this.getUserConns().then(push).catch(noop),
this.getUserCtxs().then(push).catch(noop)
])
.then(function () {
console.log(results);
})