Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJS Merge Observable in Callback

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?

like image 753
Grey Avatar asked Sep 05 '17 19:09

Grey


1 Answers

my solution:

mainFunction(): Observable<any> {

    return Rx.Observable.create(observer => {
        getAsyncWithCallback((): void => {
            getData().subscribe(observer);
        })
    })
}

mainFunction().subscribe((data) => {
    console.log(data);
})); 
like image 112
Rex Avatar answered Nov 27 '22 12:11

Rex