【问题标题】:How to handle condition inside mergeMap?如何处理 mergeMap 中的条件?
【发布时间】:2022-12-07 08:02:09
【问题描述】:
Obs1$.pipe(
  mergeMap((data) => {
    if (data.condition) {
      const generatedStuff = doSomethingFunction(data1);
      return generatedStuff.Obs2$;
    }

    someCleanupAction();

    return of(null); // I want to get rid this because I don't want to call doSomethingElse() when a failure happens
  })
).subscribe(() => {
  doSomethingElse();
})

以上是我目前基于https://stackoverflow.com/a/74552146/5195033的代码。

就像我上面的评论一样,我只是希望在条件为 true 时调用 doSomethingElse()。

【问题讨论】:

    标签: typescript rxjs observable subscribe mergemap


    【解决方案1】:

    您可以返回 EMPTY 而不是 of(null)

    import { EMPTY } from 'rxjs';
    
    Obs1$.pipe(
      mergeMap((data) => {
        if (data.condition) {
          const generatedStuff = doSomethingFunction(data1);
          return generatedStuff.Obs2$;
        }
    
        someCleanupAction();
    
        return EMPTY
      })
    ).subscribe(() => {
      doSomethingElse();
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-29
      • 2020-12-08
      • 2019-06-08
      • 1970-01-01
      • 2019-09-07
      相关资源
      最近更新 更多