I have this function that I want to run. The problem is that it doesn't wait for the two calls to finish and instantiate rule1
and rule2
so I get error that rule1.optionhead is undefined
. I have tried to have the code nested to each subscribe
but then the create execution happens more times than I want. What can I do to fix it?
To clarify,
I use this for loop because the selectedScenario
is an array of IDs which are in an input form ngValue
saveFunction() {
for (const pair of this.selectedScenario) {
this.ruleToSave = null;
this.ruleService.find(pair[0]).subscribe((ruleResponse: HttpResponse < Rule > ) => {
this.rule1 = ruleResponse.body;
});
this.ruleService.find(pair[1]).subscribe((ruleResponse: HttpResponse < Rule > ) => {
this.rule2 = ruleResponse.body;
});
const head1 = this.rule1.optionHead;
const head2 = this.rule2.optionHead;
if (
this.selectedOption === head1 &&
this.selectedOption !== head2 &&
(this.selectedWeakerOption === '' || head2 === this.selectedWeakerOption)
) {
..some code
this.subscribeToSaveResponse(this.ruleService.createRule(this.ruleToSave));
}
}
}