angular - 如果条件在 RxJS 中为真,我可以跳过所有后续运算符链吗

标签 angular typescript rxjs

是否有可能在 RxJS 5.5 中使用一些运算符来实现条件以跳过所有后续运算符并发出一些值(false)?例如,在这个 ng 服务方法中,如果 response.length === 0,我会绕过一堆 .map 链并立即提供 false 值:

getProjectDrivers(projectId): Observable<any> {
  return this.http.get('someUrl').pipe(
    // if (response.length === 0) { return false; and skip whole operator chain bellow}
    map((response: ProjectDriver[]) => response.filter((projectDriver: ProjectDriver) => projectDriver.streamName !== "AllRevenueStreamTotals")),
    map((response: ProjectDriver[]) => command => ({})
  )
)
.catch(this.handleError);

最佳答案

我想你总是可以玩 switchMap

  return this.http.get('someUrl').pipe(
    switchMap(response => {
      if (!response.length) {
        return of(null);
      } else {
        return of(result).pipe(
          map((response: ProjectDriver[]) => response.filter((projectDriver: ProjectDriver) => projectDriver.streamName !== "AllRevenueStreamTotals")),
          map((response: ProjectDriver[]) => command => ({}))
        )
      }
    }
  )

关于angular - 如果条件在 RxJS 中为真,我可以跳过所有后续运算符链吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56972898/

相关文章:

angular - 奇怪的变化检测行为 Angular 2+

javascript - 在 TypeScript 中使用分布在多个模块文件中的命名空间

typescript - React Native bug - 数字文字后面不允许直接使用标识符

javascript - 获取文本框的值

javascript - RxJS 方法导致回调 hell

angular - 用尽 RxJs retryWhen 运算符尝试后如何抛出异常?

angular - 单击对话框外部区域时关闭/隐藏对话框

angular - 为什么 RouterModule 使用 forRoot() 和 forChild()?

angular - 处理http删除

typescript :可以使用省略、可选、交集类型将数字类型分配给字符串类型