ForkJoin 2 BehaviorSubjects

后端 未结 2 1946
隐瞒了意图╮
隐瞒了意图╮ 2020-12-20 13:24

I have two behaviour subject streams what I\'m trying to forkJoin with no luck. As I imagined it gives back the two last values of it. Is this possible to implement it someh

2条回答
  •  粉色の甜心
    2020-12-20 13:40

    If you don't want (or don't know when) to call complete(), you may use combineLatest instead of forkJoin.

    With combineLatest, anytime one of the source observables (in your case your behavior subjects) emits a value, combineLatest will trigger:

    let stream1 = new BehaviorSubject(2);
    let stream2 = new BehaviorSubject('two');
    
    combineLatest(stream1, stream2)
        .subscribe(r => {
             console.log(r);
        });
    
    stream1.next(3);
    stream2.next('three');
    

    Console log:

    (2) [2, "two"] // initial state

    (2) [3, "two"] // next triggered on stream1

    (2) [3, "three"] // next triggered on stream2

    Live demo: https://stackblitz.com/edit/rxjs-qzxo3n

提交回复
热议问题