'How to emit observable upon receiving data from frokJoin subscription
So basically I want to get data from 3 observables with forkJoin and once they emit values I want to emit another observable form subscription logic, so I can subscribe to it from another place. What I have now
getOptionsArray = <T>(): Observable<any> => {
forkJoin([
get1(),
get2(),
get3()
]).subscribe(([data1, data2, data3]) => {
// some necessary assignment logic which needs to be here...
// here I want to emit observable so I can subscribe to getOptionsArray method from
// another place, but only after logic in subcription here is applied.
})
}
I know I could just return forkJoin and just subscribe to it from another place, but I need to do some assignment logic first inside this function and then once this logic is applied I want to return observable from this method to which I could subscribe from another place.
Solution 1:[1]
You have 2 options:
getOptionsArray = <T>(): Observable<any> => {
return forkJoin([
get1(),
get2(),
get3()
]).pipe(map([data1, data2, data3]) => {
...
return result;
}))
}
or
getOptionsArray = <T>(): Observable<any> => {
let subject = new Subject();
forkJoin([
get1(),
get2(),
get3()
]).subscribe(([data1, data2, data3]) => {
...
subject.next(result);
subject.completes();
});
return subject.asObservable();
}
I prefer the first one.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | viltor |
