I need a little help with making a function return an observable. I have a function (let's call it mainFunction
), that calls an async function (let's call it getAsyncWithCallback
) and executes a callback. Part of the callback is an async call that returns an observable (let's call it getData
).
getAsyncWithCallback
is part of a library and I cannot change it.
This is what I have:
mainFunction(){
getAsyncWithCallback(() => {
let myObservable = getData();
});
}
This is what I want to do:
mainFunction().subscribe((data) => {
// data is the data from getData()
});
What is the best way to achieve this, and chain in the error and completion form the inner?
my solution:
mainFunction(): Observable<any> {
return Rx.Observable.create(observer => {
getAsyncWithCallback((): void => {
getData().subscribe(observer);
})
})
}
mainFunction().subscribe((data) => {
console.log(data);
}));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With