I am facing a challenge in utilizing the outcomes of Observables from various functions. Some of these functions must be executed sequentially, while others can run independently. Additionally, I need to pass the result of the initial function to some nested ones.
Despite exploring numerous examples, I am struggling to comprehend this particular combination. The syntax seems perplexing to me.
Consider the scenario where each of the following functions returns an Observable:
saveContactDetails() : Observable<ContactDetails> {
// Returns a single observable with ContactDetails.
}
savePhones(contactId: string) : Observable<Phone[]> {
// Returns a single observable with an array of Phones.
}
saveEmails(contactId: string) : Observable<Email[]> {
// Returns a single observable with an array of Emails.
}
saveAll(): Observable<Contact> {
// TODO: Having trouble figuring out the logic/syntax here.
}
Steps:
- Invoke saveContactDetails(). Proceed only after its completion.
- Upon receiving the result of saveContactDetails(), invoke savePhones() and saveEmails(). These two functions (and potentially more) can execute concurrently without any specific order.
- Once all "child" Observables have finished processing, merge the results of all (parents and children) into a single Observable to be returned by saveAll().
I speculate that I need to utilize concatMap
and mergeMap
(or maybe forkJoin
). Nonetheless, I am unable to grasp the syntax despite consulting online documentation and examples.