【发布时间】:2019-02-25 10:59:55
【问题描述】:
我的用例场景如下:
我有一些可观察的链,在某些时候我需要从 Web 服务中获取更多信息,并基于已解决的信息,我想继续链或通过丢弃来停止它。
为了等待来自服务的信息,我使用concatMap(从流中发出的每个值都映射到服务返回的新可观察对象,我需要值,而不是可观察的值)。为了扁平化所有内部的 observables,所有的都包含在 concatMap
以下代码运行良好:
/* Begin of chain */
.concatMap<Type, Type>((pld: Type) => {
return this.appService.getInfo()
.concatMap<string, Type>((info) => {
if (someFailingCondition(info)) {
Observable.throw(`Failed`);
}
/* Pass-through operation */
return Observable.of(pld);
});
})
/* Rest of chain */
但我想放弃外部 concatMap 以支持纯 map 仅从主链的角度对纯值进行操作。我来了concatAll 解决方案:
/* Begin of chain */
.map<Type, Type>((pld: Type) => {
return this.appService.getInfo()
.concatMap<string, Type>((info) => {
if (someFailingCondition(info)) {
Observable.throw(`Failed`);
}
/* Pass-through operation */
return Observable.of(pld);
})
.concatAll()
})
/* Rest of chain */
但我想知道在concatMap-ing 内部运算符系列之后,是否有任何其他方法可以将可观察值的内部序列再次扁平化为扁平值管道?
【问题讨论】: