RxJs Observable: Execute function if empty/filtered

Either use partition like here RxJS modeling if else control structures with Observables operators

Or instead of filter use map and pipe the object if the former filter condition is true or null otherwise. so you can catch the null where ever you want in your chain with a filter.

Last option call some function in the else part of the filter function


We've had a similar case and tried it with partition as mentioned above but found it much handier to use throw here. So for your code

this.userInput.valueChanges
    .do(val => {
      if (!val || val.length < 3) {
        throw new ValueTooShortError();
      }
    })
    .switchMap(val => getDataViaHTTP())
    .do(val => this.results = val)
    .catch(() => this.results = [])
    .subscribe();

I suggest having a common event stream, creating two filtered streams, and merging the two before subscription:

var o = this.userInput.valueChanges;

var empty= o.filter(t=> t.length < 3)
.map(t=>[])

var nonempty = o.filter(t=> t.length >= 3)
    .switchMap(t=> getDataViaHTTP());

empty.merge(nonempty).subscribe(val => this.results = val);