I'd like to merge observables into one and then subscribe when ALL of the subjects have emitted a value. So far, I've tried Observable.merge and Observable.zip
public A:Subject<any> = new Subject();
public B:Subject<any> = new Subject();
public C:Subject<any> = new Subject();
public D:Subject<any> = new Subject();
public Complete:Subject<any> = new Subject();
A.next(true);
B.next(true);
C.next(true);
D.next(true);
Observable.zip(A,B,C,D).subscribe(res=>{Complete.next(true)})
But if I do above, I think the subscriber will listen to any of A,B,C or D subject to emit a value, not ALL. What would be the way to wait until A,B,C and D have all emitted a value?
You can use
Observable.forkJoin
that waits until all source Observables emit at least one value and all of them complete. But since you're using Subjects it's very likely that they don't complete so using.zip()
makes sense.The
zip()
operator emitsX
th only when all source emittedX
values. In your casezip()
will emit a single item only when allA
,B
,C
andD
emit at least one item.However, if you use
merge
it'll emit every time any ofA
,B
,C
andD
emit an item.Live demo: http://jsbin.com/hihutoj/8/edit?js,output