Extranion
Extranion

Reputation: 608

how to do second request with if statement within a observable

The goal

I want to execute one http request and also want to do a other http request that is based on a if statement. The observer need to do a emit when:

  1. firstvar and secondvar are equal and only 1 http request is executed
  2. firstvar and secondvar are not equal and 2 http request are executed

This is some pseudo code that I have

class ApiCallClass {
  methodThatReturnsObservable: Observable<{}> {
   observable = this.http.get(firstUrl);
   if (firstvar !== secondvar) {
     observable.concat(this.http.get(secondUrl);
   }
   return observable;
  }
}

Questions

We noticed that the second request is aborting. Is this something because of using concat and returning the observable?

Do I use the concat method correctly?

Upvotes: 0

Views: 72

Answers (1)

JB Nizet
JB Nizet

Reputation: 691635

concat() returns a new Observable. It doesn't mutate the original one.

So you need

observable = observable.concat(this.http.get(secondUrl);

Upvotes: 3

Related Questions