ForkJoin 2 BehaviorSubjects

假如想象 提交于 2019-12-01 16:53:28

Note what forkJoin() actually does from its documentation:

Wait for Observables to complete and then combine last values they emitted.

This means that forkJoin() emits a value when all input Observable are complete. When using BehaviorSubject this means explicitly calling complete() on both of them:

import { Observable, BehaviorSubject, forkJoin } from 'rxjs';

const stream1 = new BehaviorSubject(2);
const stream2 = new BehaviorSubject('two');

forkJoin(stream1, stream2)
  .subscribe(r => {
    console.log(r);
  });

stream1.complete();
stream2.complete();

See live demo: https://stackblitz.com/edit/rxjs-9nqtx6

March 2019: Updated for RxJS 6.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!