I want to achieve a scenario using rxjs where a group of http calls are made with the value returned from a previous call. The inner calls should run in parallel, while still being able to return the value from the first call as soon as it's available. Subscription to the result and error handling are also necessary. Importantly, errors from inner calls should not stop sibling calls.
The current code I have works fine but waits to return the initial value until all inner calls are complete due to using forkJoin
:
public create(customer: Customer, groupIds: number[]): Observerable<Customer> {
const response = this.http.post<Customer>('/customer', customer).pipe(mergeMap(
(created) => {
return forkJoin([
of(created),
forkJoin(groupIds.map(groupId => {
const membership = new Membership(created.Id, groupId);
return this.http.post<Membership>('/membership', membership);
)
]);
}
)).pipe(map(a => a[0]));
return response;
}
Is there a way to operate such that the created customer is returned without waiting for inner calls to finish? Could the above code be written more succinctly?
(NB: this.http
refers to type HttpClient
from Angular, which returns an Observable<T>
)