在rxjs 5.5中,是否可以使用某些运算符来实现条件,从而跳过所有后续运算符并发出某些值(false)?例如,在这个ng服务方法中,如果出现以下情况,我将绕过这个.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 => ({}))
        )
      }
    }
  )

09-18 19:50