简体   繁体   English

如何在从 frokJoin 订阅接收数据后发出可观察的

[英]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.所以基本上我想用 forkJoin 从 3 个 observable 获取数据,一旦它们发出值,我想发出另一个 observable 表单订阅逻辑,这样我就可以从另一个地方订阅它。 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.我知道我可以返回forkJoin并从另一个地方订阅它,但我需要先在这个 function 中做一些赋值逻辑,然后一旦应用这个逻辑,我想从这个方法返回 observable,我可以从另一个方法订阅地方。

You have 2 options:您有 2 个选择:

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.我更喜欢第一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM