I am kind of new to Angular 4 and Observables
and everything related to it.
I am trying to execute two http
requests one after another (only when the first one succeeds).
I'm using this code:
public getCompany(id: string): any {
let company = null;
this.authService.isValidUser().subscribe(response => {
const token = this.tokenStorageService.getTokenFromStorage();
const requestUrl = environment.gatewayUrl + environment.companyService + environment.getCompanyEndPoint + id;
const headers = new Headers();
headers.set('Authorization', 'Bearer ' + token);
const options = new RequestOptions({headers: headers});
return this.http.get(requestUrl, options).catch(this.errorService.handleError);
}, (error: AppError) => {
// ........ //
});
}
This is the isValidUser()
method code:
public isValidUser(): any {
const token = this.tokeStorageService.getTokenFromStorage();
if (!token) {
console.log('cannot find token');
return false;
}
const requestUrl = environment.gatewayUrl + environment.authorizationService + environment.userServiceEndPoint;
const headers = new Headers();
headers.set('Authorization', 'Bearer ' + token);
const options = new RequestOptions({headers: headers});
return this.http.get(requestUrl, options).catch(this.errorService.handleError);
}
The idea here is to only return return this.http.get(requestUrl, options).catch(this.errorService.handleError);
after the authService.isValidUser()
code worked.
I don't think this is a proper way of performing such requests, since my second request is already completed before the first one does.
Maybe I am missing some way on how to do it properly?
Thank you.