I am creating a Work Item object with linked child Task objects. My function (createWorkItemAndChildren) takes two parameters, the workItem, and an array of Task objects. I want my function to return an array of all ids that are created (work item and tasks).
I have to have the parent ID back from one http POST call (workItemService.createWorkItem) before I can then create the child tasks which use another http POST method on the same service.
I now have the forkJoin in createChildWorkItems returning all of the child ids at once.
How do I refactor this so that there is only one subscribe, and to return the array with parent and child ids together?
createChildWorkItems(parentId, tasks: Task[]): Observable<any> {
return <Observable<number>> forkJoin(
tasks.map(task => <Observable<number>> this.workItemService.createChildWorkItem(parentId, task))
).pipe(zip());
}
createWorkItemAndChildren(workItem, childTasksToSave: Task[]){
var resultArray = [];
this.workItemService.createWorkItem(workItem).subscribe(workItemId => {
var parentId = workItemId;
resultArray.push(parentId);
if (parentId !== null){
this.createChildWorkItems(parentId, childTasksToSave).subscribe((results: number) => {
resultArray.push(results);
this.tfsIdsCreated = resultArray;
});
}
});
}