Я думаю, что вы хотите соединить оба вызова с помощью карты слияния. Я изменил ваш пример, чтобы показать, что делать.
Прежде всего, ваш метод fetchLists
не должен подписываться на службу списков, а только конвертировать данные и возвращать полученную наблюдаемую. Затем в вашем методе getSocialDataTwitter
вы будете вызывать fetchLists
и использовать mergeMap для цепочки вашего второго http-вызова.
fetchLists(searchQuery) {
// Make note how we map the result and then return the observable
// without subscribing.
return this.listService.getLists()
.pipe(
map((data: List[]) => data.find(d => d.name === searchQuery))
)
}
getSocialDataTwitter(searchQuery) {
// As the headers and url will not change, we can define them here
const headers = new HttpHeaders().set('content-type', 'application/json');
const url = this.baseUrl + 'api/OperationData/GetAllTweetsByNamePost';
// Here we call our initial method, but instead of subscribing directly
// we use mergeMap to chain our observables
this.fetchLists(searchQuery)
.pipe(
// Lists is the result we created with the map in fetchLists.
mergeMap(lists => this.http.post<RightOperatorPaneltwitter>(url, lists, { headers }))
)
// Only after we subscribe here, the list service is called.
.subscribe(result => this.tweets = result);
}