Within my Angular 6 application, there is a service where I declare a variable named "permittedPefs". This variable is asynchronously set within an httpClient.get call.
@Injectable()
export class myService implements OnInit {
permittedPefs = [];
constructor(){}
ngOnInit() {
// STEP 1
this.loadUserPefsService.getUserRolePefs(roleId).subscribe(
(returnedListPefs) => {
this.permittedPefs = returnedListPefs;
},
error => {
console.log(error);
});
}
// STEP 2
this.myMethod(1);
Subsequently, a method is called that relies on the supposed value of "permittedPefs".
myMethod(pefId): boolean {
return this.permittedPefs.includes(pefId);
}
The issue arises when it appears that "permittedPefs" has not yet been assigned a value, causing the method call to reference an incorrect value. How can this be resolved without necessitating a call from within the http Response callback (since it is used in multiple places)?
Any suggestions or solutions?