Hi, i’ve been working on app and im looking a better way to handle this issue, hope its not a noob question:
I have a ResourceService under /providers and there I handle all my api calls, ie:
export class ResourceService {
headers:Headers = new Headers();
token: any;
constructor(
public http: Http,
public storage: Storage
) {
this.getToken().then(val => {
if(!this.token) {
this.token = val;
}
});
}
public getToken() {
return this.storage.get("token");
}
interceptor(withToken = false){
const opts:RequestOptions = new RequestOptions();
opts.headers = new Headers();
opts.headers.append('Content-Type', 'application/json');
if (withToken) {
opts.headers.append('Authorization', 'Bearer ' + this.token);
}
return opts
}
getAddressList(clientId:any) {
return this.http.get(`${API_ROOT}/clients/${clientId}/address`, this.interceptor(true))
.map(res => res.json())
.catch(this.handleError);
}
so my problem is when i call getAddressList if the “this.getToken()” from the constructor hasn’t resolved yet the token is null when i call “this.token” on the interceptor() method.
Im looking for a better way to handle this situation. Any help / direction would be really helpful.
Thanks in advance!