I want to construct a function that creates and returns an Observable. Depending on the parameters this function receives it should either create the Observable and return it directly or (in a special case) call another asynchronous function before returning the Observable.
Abstract example:
async function doSomethingAsync(){
return 'success';
}
function returnObservable(specialCase: boolean): Observable<any>
const observable = new Observable(
observer =>
observer.next(1)
}
);
if(specialCase) {
doSomethingAsync().then(
then => {
// this does not work, of course, but that's what I would like to be able to do
return observable;
}
)
} else {
return observable;
}
}
Now my problem is that appearently I cannot call an asynchronous function and then return the Observable. I could make this whole returnObservable function asynchronous and just await the doSomethingAsync but then the returnObservable function would return a Promise returning an Observable - and that's not what I want. The consumer of this function should receive the Observable directly.
I hope I could make my problem clear. Is there any way to solve this problem?
Observable), there's hope. :-)