I have a service used as a local data store to share data across the app. This service makes a few rest calls in the beginning to initialize some data. It look like this:
Injectable()
export class DataStoreService {
leadList: LeadModel[]
optyList: ContactModel[]
constructor(public dataService:DataService){
this.initializeData()
}
initializeData() {
this.dataService.getObjectData('opportunities').subscribe(
(data) => this.optyList = data
)
this.dataService.getObjectData('leads').subscribe(
(data) => this.leadList = data
)
}
}
I have a component page where I do below:
ngOnInit() {
for(let lead of this.dataStore.leadList){
if(lead.accepted == false)
this.newLeadsList.push(lead)
}
}
It is very obvious that if initialize data fails to finish the leadList may be undefined and this ngOnInit for loop will crash as well. So, how do I wait in the ngOnInit until initializeData finishes?