Reputation: 243
I want to achieve something like this
MyComponent.ts
getData() {
servicemethod.subscribe(data => this.result=data)
// use this.result for further operations.
}
This is my service component.
MyService.service.ts
serviceMethod(args) {
let result; // best suitable data structure to store the response may be an array or map
result1 = _http.get(url1).map((res:response) => res.json);
result2 = _http.get(url2).map((res:response) => res.json);
add result1 and result2 in result and return it in such a way that it easily retrievable at calling end.
}
I want the result of both request in single subscription.Is it possible? I am using angular 4.0.0 and the http requests are rest api calls in java.
Upvotes: 2
Views: 58
Reputation: 28692
use forkJoin
serviceMethod(): Observable<any> {
result1 = _http.get(url1).map((res:response) => res.json);
result2 = _http.get(url2).map((res:response) => res.json);
return Observable.forkJoin([result1 , result2 ])
.map(responses => {
// responses[0] => result1
// responses[1] => result2
});
}
Upvotes: 2