0

I have some component code that looks like this:

private myThing: MyThing;

ngOnInit() {
    this.service.getMyThing().subscribe(thing => {
        this.myThing = thing;
    }
}

However, the service looks like this - it is composed of data joined by two http requests. The first one is from "otherService", which fetches an additional property for the thing object, and the second call fetches the base thing object. I would like to combine these results and alert the subscriber when all action has been completed, i.e. this.myThing should have the ".prop" property...

public getMyThing(): Observable<MyThing> {
    this.otherService.getThingProperty().subscribe(prop => {
         return this.http.get('url')
             .map(res => res.json())
             .map(thing => {
                 thing.prop = prop;
             });
    });
}

But this is clearly wrong - the function thinks it is void which makes sense... I just don't know how to get the component to subscribe to the final observable. Any tips are appreciated. Thanks!

1 Answer 1

1

Try switchMap:

public getMyThing(): Observable<MyThing> {
  return this.otherService.getThingProperty().switchMap(prop => {
     return this.http.get('url')
         .map(res => res.json())
         .map(thing => {
             thing.prop = prop;
         });
  });
}
Sign up to request clarification or add additional context in comments.

1 Comment

The switchmap looks to be exactly what I was looking for...I realized I am missing a piece to my puzzle - do you have any insight for this question: stackoverflow.com/questions/38712303/…

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.