0

I have this function:

someFunction(): Promise<Observable<boolean>> {
  return Promise1()
           .then(text => Promise2(text))
           .then(resultString => Observable(resultString))
}

Where Promise1 and Promise2 are functions that return promises and Observable is a function that returns an observable.

So to get an observable value I have to do the next call:

someFunction().then(res => res.subscribe(value => //get Value));

Is there a way to change someFunction to get the value directly from an observable?

someFunction().subscribe(res => //getValue);
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
nothing
  • 49
  • 2
  • 8
  • Did you read e.g. https://stackoverflow.com/q/39319279/3001761? – jonrsharpe Oct 12 '21 at 15:43
  • Why? Why not just work with RxJs where you can use constructs like map, switchMap, zip, etc that make it much easier to understand what it is you are writing (and more importantly what did you write later on when you have to change the code). Your fellow developers/team mates will thank you for it. – Igor Oct 12 '21 at 15:45

1 Answers1

1

Try something like this?

  • from converts a promise to an observable
  • switchMap does this conversion as well, so you can just return a promise from within that lambda
someFunction(): Observable<boolean> {
  return from(promise1()).pipe(
    switchMap(text => promise2(text)),
    switchMap(resultString => observable1(resultString))
  );
}
Mrk Sef
  • 7,557
  • 1
  • 9
  • 21