I am in the process of developing a new web application service. The first step involves obtaining a token through the rest API. Once this token is obtained, it needs to be sent as a header to retrieve additional information.
The issue I'm facing is that the function to fetch more information is being triggered before the token variable is properly set (resulting in an empty header). However, if I manually set the token, everything works correctly.
Is there a way to ensure that my function waits until the variable has been assigned a value?
Thank you
gettoken(): void {
this.http.get<InitResponse>('URL' + '/token').subscribe(data => {
this.token = data.token;
localStorage.setItem('token', data.token);
getmoreInfo();
},
(err: HttpErrorResponse) => {
if (err.error instanceof Error) {
console.log('Client-side error occured.');
} else {
console.log('Server-side error occured.');
}
}
);
}