本文介绍了angular2 rxjs可观察到的forkjoin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
即使其中一个请求失败,是否也可以继续forkjoin http.get请求.
Is it possible to continue forkjoin http.get requests even if one of the requests fails.
我正在寻找类似的$ q.allSettled在angular2中的功能.
I'm looking to a similar function of $q.allSettled in angular2.
请参阅示例: http://jsfiddle.net/Zenuka/pHEf9/
angular.module('qAllSettled', []).config(function($provide) {
$provide.decorator('$q', function($delegate) {
var $q = $delegate;
$q.allSettled = function(promises) {
return $q.all(promises.map(function(promise) {
return promise.then(function(value) {
return { state: 'fulfilled', value: value };
}, function(reason) {
return { state: 'rejected', reason: reason };
});
}));
};
return $q;
});
});
卡布
推荐答案
在这种情况下,您可以利用catch
运算符为每个可观察对象拦截错误并返回另一个可观察对象.
You could leverage the catch
operator for each observable to intercept the error and return another observable in such cases.
以下是示例:
return Observable.forkJoin(
this.http.get('/some-url')
.map((res:Response) => res.json())
.catch(res:Response => Observable.of({}),
this.http.get('/some-other-url')
.map((res:Response) => res.json())
.catch(res:Response => Observable.of({}),
);
这篇关于angular2 rxjs可观察到的forkjoin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!