I'm still learning observables, so I won't be surprised if there's an easy solution. Basically what I have right now is four nested subscriptions with a fourEach inside the second subscribe(). I saw lots of answers using switchMap, but I couldn't find one that also had a for loop to iterate through. I know I probably should be using nested subscriptions, but I can't figure out how to do it with the forEach.
This is the working code with the nested subscribes:
dialogRef.afterClosed().subscribe(result => {
if(result) {
this.createLikertResponseGroup(result.likertResponseGroup)
.subscribe(likertResponseGroupJSON => {
result.likertResponse.controls.likertResponseFormArray.controls.forEach((element) => {
let characteristic = element.controls.characteristic;
this.newResponseGroupId = likertResponseGroupJSON.LikertResponseGroup.id;
this.createLikertResponse(element, this.newResponseGroupId)
.subscribe(likertResponseJSON => {
if (characteristic) {
let responseId = likertResponseJSON.LikertResponse.id;
this.createCharacteristic(characteristic, this.newResponseGroupId, responseId)
.subscribe(characteristicJSON => {
this.newCharacteristicId = characteristicJSON.Characteristic.id;
});
}
});
});
})
}
});
What I have works right now. So my question is, is it worth changing how I'm doing this? If so, how would I go about it?
I haven't gotten far, but my attempt with switchMap looks like this:
dialogRef.afterClosed().pipe(
filter(result => result != null),
switchMap(result =>
from(result.likertResponse.controls.likertResponseFormArray.controls).pipe(
// not sure what to do after this (or if I'm even doing it right)
)
),
);