0

How do I make this line return a value in the subscribe ? It is returning an observable instead:

let test = undefined;
of(test, this.myService.whatever(var1, var2))
    .pipe(
        first(n=>!!n) 
    ).subscribe(result=>console.log(result)); // returns Observable, should return value.

myservice.whatever returns an observable. I want to make it resolve inside pipe. Something like this:

let test = undefined;
of(test, this.myService.whatever(var1, var2))
    .pipe(
        first(n=>!!n),
        getValueFromObserver() // if it's an observer get the value
    ).subscribe(result=>console.log(result)); 

Is this possible ? What I'm trying to do is check inside the observer's sequence if a local variable has a value and if it doesn't, then get the value from the backend. I want to make this because the real code is far more complex and the result of this observable will be forkJoined with other observers.

Edric
  • 24,639
  • 13
  • 81
  • 91
Nelson Teixeira
  • 6,297
  • 5
  • 36
  • 73

1 Answers1

1

haven't fully tested but theoretically, you can check instanceof

let test = undefined;
of(test, this.myService.whatever(var1, var2))
    .pipe(
        first(n => !!n),
        switchMap(value => value instanceof Observable ? value : of(value))
    ).subscribe(result => console.log(result)); 
Poode
  • 1,692
  • 1
  • 15
  • 20
Fan Cheung
  • 10,745
  • 3
  • 17
  • 39