I am faced with the task of creating a parent record followed by multiple child records (order does not matter), and ending with a logging action.
I am knowledgeable on how to chain single actions on an observable by mapping them together. For example:
-- create one parent record and return its ID:
createParentRecord(parentInfo: parentType): Observable<number>
-- create one child record and return its ID
createChildRecord(childInfo: childType): Observable<number>
-- log the entry
logStuff(parentId: number, childId: number)
-- the composite function executes all the actions and returns the parent ID
doTheThing(): Observable<number> {
/* ... */
let parentId: number;
let childId: number;
return createParent(parentInfo).pipe(
tap(id => (parentId = id)),
mergeMap(id => { childInfo.parentId = parentId; return createChildRecord(childInfo);} ),
mergeMap(childId => { childInfo.childId = childId; return logStuff(parentId, childId); ),
map( () => parentId)
);
}
Question
This method works perfectly.
However, if I have an array of childInfo[] instead of just childInfo, how do I approach this? How can I handle an array of childInfo[] to subscribe to and retrieve inner observable results? I simply want a count of successful results or any results returned by each observable. Should I use forkJoin()?
I am struggling to find a solution to this. Can someone offer some assistance?