代码之家  ›  专栏  ›  技术社区  ›  Simon_Weaver

RxJS'combinelast'如果所有输入不能立即可用,则会出错

  •  0
  • Simon_Weaver  · 技术社区  · 6 年前

    我经常用 combineLatest 把三个或四个可观测值结合起来计算一个简单的条件。

    如果其中一个“组合”的观测值没有发射,那么它将阻塞直到它发射为止。如果稍后一直发出的东西立即停止发出,那么调试可能会非常棘手。

    尤其是在屏幕上,很难观察到它的位置。

    所以我需要的是 combineLatestImmediate

    combineLatest(obs1, obs2, obs3)
            .pipe(timeoutWith(0, throwError('Expected obs all to be available')))
    

    甚至:

    // note: take(1) is required or the wrong observable
    // may get reported if the source hasn’t closed
    combineLatest(obs1.pipe(take(1), timeoutWith(0, throwError('obs1 has no value'))),
                  obs2.pipe(take(1), timeoutWith(0, throwError('obs2 has no value'))), 
                  obs3.pipe(take(1), timeoutWith(0, throwError('obs3 has no value'))));
    

    为了节省调试时间,这是一个安全/正常的事情。

    我也不是在找 forkJoin (哪个 short-circuits

    0 回复  |  直到 6 年前