ankitjain11 ankitjain11 - 1 year ago 102
Javascript Question

Wait for a stream before invoking another stream

How do I wait for one stream (say,

) to return non-null value and then invoke
subscribe function. I'm not particularly interested in
's value. In turn, I am trying to get
's value which might have been updated before the StreamA returned non-null value, and might not have any new events.

I tried, pausable, by looking at this: RxJS: How can I do an "if" with Observables?, but unfortunately could not get it to work. This is because, there are no exported class
, rxjs v 5.0.0-beta.6.

This is how far, I've come up with, as per the answer.

export class AuthService {
userModel: FirebaseListObservable =
constructor(private af: AngularFire) {

var user = this.currentAuthor();
var userStream = user; => { console.log(d);return this.userModel.publishReplay(1); });

.subscribe((data) => {
var flag = false;
data.forEach((item) => {
if (item.$key && item.$key === user.uid) {
flag = true;

if (flag) {
} else {
firstName: user.auth.displayName.substr(0, user.auth.displayName.lastIndexOf(' ')),
lastName: user.auth.displayName.substr(user.auth.displayName.lastIndexOf(' '), user.auth.displayName.length),
displayPic: user.auth.photoURL,
provider: user.provider,
uid: user.uid
public currentAuthor():FirebaseAuthState {

Hope, I can make myself clear. Even I am getting confused now. :p.

I am new to rxjs and reactive programming. And, any help will be appreciated.

And, btw, thanks for stopping by. :)

Answer Source

I suppose by plausible you mean pausable? I am not sure what exactly you are trying to achieve here (control flow?). However, if you want streamB value after streamA produces a value, then you can use flatMap.

streamA.flatMapLatest(function (_){return streamB})

That should give you, anytime streamA emits, the values emitted after that time by streamB.

If you want values including the last one B emitted prior to that time, you can use streamBB = streamB.publishReplay(1) and

streamA.flatMapLatest(function (_){return streamBB})

Haven't tested it, so keep me updated if that works.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download