1

I'm totaly new in async Observable world. I hope somebody help me.

I have two Observable<boolean> and I want to combine them.

I tried to use:

var obs1 = Observable.of(true).delay(1000);
var obs2 = Observable.of(false).delay(100);    
obs1.combineLatest(obs2, (obs1Val, obs2Val) => {
    //bool result
    });

and thats almost it... almost because I want to start obs2 when obs1 is finished, with combineLatest both starts in the same time.

obs1 and obs2 here are simple examples in my case thats angular http requests:

obs1 : Observable<boolean> = http.get(...).map(d => <boolean>d);

Thanks in advance for help

1
  • Why not subscribe to the 1st obs1 and make the other call form the callback? Commented Sep 10, 2016 at 19:54

3 Answers 3

1

If you want to start obs2 when you get response from obs1, it has to be called in onNext of obs1.subscribe.

getBothResult() {
  return Rx.Observable.create(function (observer) {
    obs1 : Observable<boolean> = http.get(...).map(d => <boolean>d);
    obs1.subscribe((obs1Value) {
      Observable.of(false).delay(100).subscribe(obs2Value => {
        //compute value from obs1Value and obs2Value as they are visible in closure assign it to eg. finalValue
        var finalValue = obs1Value + obs2Value;
        observer.onNext(finalValue);
      });  
    });
  });
}    

It's very possible that an operator exists that does all those things in some nifty way, however I'm not aware of it.

Sign up to request clarification or add additional context in comments.

3 Comments

Sth like that but I want to wrap your code in another function which return Observable<boolean> calculated from obs1 and obs2 values. I tried like there but it doesn't work: jsfiddle
obs2 triggers after obs1 completes. getBothResult() wait for both. Finally getBothResult() return Observable<boolean> calculated from obs1Val and obs2Val. Thats what I need.
that's it! In my case I had to call observer.complete() after each next() also. Thanks a lot!
1

You can use observable zip

 let obs1 = Observable.of(true).delay(1000);
 let obs2 = Observable.of(false).delay(100);    

 let source = Observable.zip(
      obs1,
      obs2,
      (ob1, ob2) => {
        return {
          value1: ob1,
          value2: ob2
        };
      }
  );
  source.subscribe(data => console.log(data))

3 Comments

According to RxJS documentation I'm afraid that obs2 won't wait for obs1 complete before start :/
Tested. As I expected both observables started in the same time :/
Hey thy dont start at same time thy will be only resolved at same time
0

As you want to have obs2 execute after obs1, we can wait for a response In the Rx world, you should avoid subscribing to an observable, and then subscribing to another observable in the callback.

Here is an approach that should get you the result you are looking for.

var obs1 = Observable.of(true).delay(1000);
var obs2 = Observable.of(false).delay(100);
var obsOf1And2 = obs1.flatMap(obs1Response => 
    obs1Response.flatMap(obs2Response => { 
        // do what you want with both responses 
    }));

obsOf1And2.subscribe(console.log);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.