I have a parent record I need to create, and then a series of child records to create (order not important). Then I need to do a follow-up logging action.
I know how to create a chain of single actions on an observable by mapping them together, so, I could do:
-- create one parent record, return its id:
createParentRecord(parentInfo: parentType): Observable<number>
-- create one child record, return its id
createChildRecord(childInfo: childType): Observable<number>
-- log the entry
logStuff(parentId: number, childId: number)
-- composite function does all the work, returning the parentId
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 works great.
BUT if I have childInfo[] rather than childInfo, how do I do this? how do I handle an array of childInfo[] to subscribe to and return inner observable results? I just want a number back of how many succeeded - or just whatever results come back from each observable? Do I use forkJoin()?
I can't for the life of me figure this out. Help?