Imagine you have two distinct subjects s1
and s2
each with their own subscriptions,
const s1 = new Subject<number>();
const s2 = new Subject<number>();
s1.subscribe({
next: (value) => console.log("s1 emitted", value),
complete: () => console.log("s1 completed"),
});
s2.subscribe({
next: (value) => console.log("s2 emitted", value),
complete: () => console.log("s2 completed"),
});
Now, if we take an observable like of(1)
, we can pass it through s1
using the following code:
of(1).pipe(tap(s1)).subscribe();
This will result in the output:
s1 emitted 1
s1 completed
However, what if you wanted to process the data through both s1
and s2
simultaneously?
One way to achieve this is by:
of(1)
.pipe(tap((x) => [s1, s2].forEach((s) => of(x).subscribe(s))))
.subscribe();
which would generate the output:
s1 emitted 1
s1 completed
s2 emitted 1
s2 completed
Is there a more efficient or concise approach for achieving this? I've experimented with merge
but without success.