I am facing an issue with a function that I need to run. The problem is that it does not wait for both calls to finish and instantiate rule1
and rule2
, leading to an error stating that rule1.optionhead is undefined
. I attempted nesting the code within each subscribe
but then the execution occurs more times than intended. How can I resolve this?
In order to provide clarity,
I am using this for loop as the selectedScenario
contains an array of IDs which are part of an input form using 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));
}
}
}