1
private getScmServiceMeta(serviceId: string): Observable<any> {
    let service = this.getServiceItem(serviceId);

    if (service) {
        return this.httpClient.get(serviceUri).map((res: Response) => {
                return _.get(res, 'data.serviceinfo');
            })
    } else {
        return Observable.throw('backend server error');
    }
}

public inquireScmData(serviceId: string, params: object, templateId: string): Observable<any> {
    return this.getScmServiceMeta(serviceId).map((serviceInfo: any) => {

        let uri = serviceInfo.routeTemplate;

        // How to return the _.get(res, 'data') as an Observable ???
        return this.httpClient.get(uri).map((res: Response) => {
            if (_.startsWith(_.get(res, 'status.code'), '200')) {
                return _.get(res, 'data');
            } else {
                throw ('status code error');
            }
        })
    })
}

as the above code, I need to return an observable when call the function 'inquireScmData', but inside the function, it need to call another function 'getScmServiceMeta' which is also asynchronous and return also an Observable, so how to return the '_.get(res, 'data')' as an Observable??? thanks

1
  • 1
    return Observable.of(_.get(res, 'data') ) Commented Sep 29, 2017 at 6:51

1 Answer 1

1

rxjs gives you a static method on Observable to do this.

Observable.of( _.get(res, 'data') );

Though, you'll then have an Observable<Observable<Observable<any>>>. Is that what you are intending, or do you just want to chain "async" operations. If so, you should maybe leave your code as is, and use mergeMap.

public inquireScmData(serviceId: string, params: object, templateId: string): Observable<any> {
  return this.getScmServiceMeta(serviceId).mergMap((serviceInfo: any) => {

    let uri = serviceInfo.routeTemplate;

    return this.httpClient.get(uri).map((res: Response) => {
      if (_.startsWith(_.get(res, 'status.code'), '200')) {
        return _.get(res, 'data');
      } else {
        throw ('status code error');
      }
    })
  })
}
Sign up to request clarification or add additional context in comments.

1 Comment

shouldn't he use switchMap in this case?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.