2

I have a function that does something like the following:

async foo() {
    await doSomething();

    return getObservableFromSomewhereElse();
}

The problem is that the await makes the signature a Promise<Observable<T>>. Because I'm already returning an Observable, I'd just like to fold the Promise into the Observable so that the signature is Observable<T>.

Note: doSomething() must occur before getObservableFromSomewhereElse().

I think Observable.defer() might help with this but I'm not certain.

Any ideas on how to make this function just return Observable<T>?

msanford
  • 11,803
  • 11
  • 66
  • 93
MgSam
  • 12,139
  • 19
  • 64
  • 95
  • 1
    It can't just return an `Observable`, `async` functions must return a promise or something that gets wrapped in a promise. It *could* return a `Promise`, just use `.toPromise()`. – jonrsharpe Apr 11 '18 at 15:25
  • Hm, the question is a bit different from "How can I `await` on an Rx Observable?". It is more "How to wait for promise and emit observable after that". – udalmik Apr 11 '18 at 15:37
  • @jonrsharpe I don't want it to return a promise. It provides a stream of values (thus an Observable). And the function doesn't need to be `async`. Using something like `Observable.defer` the async function could be an inner function passed to Observable and the outer function is then not async. – MgSam Apr 11 '18 at 15:38
  • 1
    Then you can't use an async function, just return the observable. – jonrsharpe Apr 11 '18 at 15:38
  • 1
    There should be something similar to `Observable.fromPromise(foo()).flatMap(any => getObservableFromSomewhereElse());` – udalmik Apr 11 '18 at 15:44
  • 3
    Just asserting isn't a particularly helpful response. Please explain *why* the duplicate doesn't solve the problem. If returning a promise isn't what you want, why use an async function to start with? If you want to convert a promise to an Observable, rather than vice versa, *search for that*. And, as always, please [be nice](https://stackoverflow.com/help/be-nice). – jonrsharpe Apr 11 '18 at 15:48
  • There's no ˋawait´ without ˋasyncˋ, the god of syntactic sugar says. – aventurin Apr 11 '18 at 20:58
  • @udalmik Thank you for the answer. If you make it a real answer I'll accept it. – MgSam Apr 14 '18 at 16:48
  • @jonrsharpe No you have it completely backwards. It is not the responsibility of the people using the site looking for **help** to **prove** they don't have a duplicate. It's the responsibility of the self-appointed "moderators" to prove it is a duplicate. You and others routinely close and vote down questions without even bothering to read them first. Your moderation is little more than repeating what the "similar questions" algorithm already does. My question was perfectly clear as evidenced by the only real answer I got, from udalmik, answered it correctly. – MgSam Apr 14 '18 at 16:53
  • Three people voted to close against that duplicate. I'm not sure whose responsibility you think it is to make your questions clear, or why you're so opposed to actually clarifying it in an edit. But if you have a problem or query, I'd suggest you take it to [meta]. – jonrsharpe Apr 14 '18 at 17:21

1 Answers1

2

As mentioned in comments thread - you can leave foo function as async and reuse its promise result in separate function with

async function foo() {
    await doSomething();
}

function bar() {
    return Observable
       // emits one value from resolved promise
       .fromPromise(foo())
       // maps it to your observable stream
       .flatMap(() => getObservableFromSomewhereElse());
}
udalmik
  • 7,838
  • 26
  • 40