组合流

RxJS combineLatestWith

所有输入至少发出一次后,任意一方更新都会组合双方最新值。

source$.pipe(combineLatestWith(other$))

时间线如何变化

  1. 1
    first$ 先发出 Ada

    last$ 还没有值,因此暂不输出。

  2. 2
    双方都有首值

    组合为 Ada Lovelace。

  3. 3
    first$ 更新

    使用 last$ 的最新值组合 Grace Lovelace。

  4. 4
    last$ 更新

    任意一方更新都会产生新的组合。

受支持的示例代码

实验室只根据下列示例中的关键操作符生成预设时间线,不执行任意输入代码。

import { combineLatestWith, map } from 'rxjs'

const fullName$ = firstName$.pipe(
  combineLatestWith(lastName$),
  map(([first, last]) => `${first} ${last}`)
)

fullName$.subscribe(console.log)