I have 5 functions which will return data. I have to execute all these functions before executing the 6 th function. Currently, it is executing the 6th func before all 5 functions return data. This happens only the first time. The second time,when 6 th function is executing,it has all the data from the 5 functions.
ngOnInit()
{ this.getTitle(); //func 1
this.getNation();
this.getGender();
this.getPass();
this.getPax(); //func 5
this.patchFormValue(); //func 6
}
private getTitle() { // 5 similar functions that return data
this._addNewPassengerService.getTitle()
.pipe(takeUntil(this.componentDestroyed$))
.subscribe(
resData => {
this.titleData = resData || [];
this._changeDetector.detectChanges();
}
);
}
private patchFormValue() {
const data = this.data.passengerDetails;
if (data && this.titleData && this.nationData
&& this.paxData &&this.genderData && this.passengerData) {
this.addPassengersFormGroup.controls.passengerFormArray['controls'].forEach(group =>
{
group.patchValue(data);
});
this._changeDetector.detectChanges();
}
}