I have 2 API calls in a service that each return an Observable, and in my component I have some condition that, if true, I must call both those functions, but I need to wait for the get() call, so that I can execute the post function with arguments returned from the get call. If false, I just want to call the post function with the already defined arguments.
service:
get(id: string) {
return this.http.get<any>('get path');
}
post(data: data) {
return this.http.post<any>('post path', {data: data});
}
component:
execute() {
if (condition) {
this.service.get(id).subscribe( (res) =>
// ...
this.data = res.data;
post(data).subscribe(() => // do stuff...);
);
} else { post(data).subscribe(() => // do stuff...); }
}
I want to not repeat code for the post call, or if not at all possible, just not use a subscribe() inside another subscribe(). How can I do this? without async await.
Thanks in advance