【问题标题】:returning non-observable flat value from nested concatMap从嵌套的 concatMap 返回不可观察的平面值
【发布时间】: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 内部运算符系列之后,是否有任何其他方法可以将可观察值的内部序列再次扁平化为扁平值管道?

【问题讨论】:

    标签: rxjs rxjs5


    【解决方案1】:

    你的代码已经很简单了,有时候不可能把所有的 observable 都扁平化成一个链,你可以进一步简单地跟随它,不需要 concatAll

    /* Begin of chain */
            .concatMap<Type, Type>((pld: Type) => {
                return this.appService.getInfo()
                .map<string, Type>((info) => {
                    if (someFailingCondition(info)) {
                      throw(`Failed`);
                    }
                    /* Pass-through operation */
                    return pld;
                })
            })
    /* Rest of chain */
    

    【讨论】:

      猜你喜欢
      • 2015-06-20
      • 2021-09-22
      • 1970-01-01
      • 2022-10-15
      • 2020-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-26
      相关资源
      最近更新 更多