flatMap之后如何获取结果并抛出带有结果的新Observer?

时间:2018-11-01 16:01:32

标签: rxjs reactive-programming observers flatmap

我有以下Observer方法:

public __init__(httpResponse: HttpResponse<any>): Observable<any> {
    return this.initializeCredentials(httpResponse.headers, httpResponse.body['result']).flatMap((res) => {
      return this.initializePeriod(res);
    });

initializeCredentials()返回观察者的地方:

private initializeCredentials(headers: HttpHeaders, body: any): Observable <any> {
   return Observable.of({'a' :1});
}

然后initializePeriod()返回观察者:

private initializePeriod(profile: any): Observable<Period> {
   return return Observable.of({'b' :2});
 }

如何在第二个观察者:return this.initializePeriod(res);之后获得结果并将结果扔到方法__init__()的更远处?

通知:将结果从第一个观察者传递到第二个this.initializePeriod()很重要。

结果,我需要订阅__init__()并从Observerbes获得两个结果:

this.__init__().subscribe((result) => {
      console.log(result);
});

1 个答案:

答案 0 :(得分:1)

如果您使用的是rxjs5,则为flatMap提供一个结果选择器功能

public __init__(httpResponse: HttpResponse<any>): Observable<any> {
    return this.initializeCredentials(httpResponse.headers, httpResponse.body['result']).flatMap((res) => {
      return this.initializePeriod(res);
    },
(firstRes,secondRes)=>{
return {firstRes,secondRes}
}
);