Hello!
I want to read the token from localstorage in the constructor to build the header for http calls. My problem is that because reading from storage takes a while, my function which uses the token will be executed before the constructor.
How can I resolve this?
I tried with promise with no luck, my latest code is the following:
contentHeader: Headers;
constructor(public http: Http, public storage: Storage) {
console.log('Hello Announcements Provider');
this.getLocalStorageData()
.then(data =>{
//ok
})
}
getLocalStorageData(){
return new Promise(resolve => {
this.storage.get('token').then((value) => {
this.contentHeader = new Headers({
"Content-Type": "application/json",
"Authorization": "Bearer " + value
});
resolve(value);
});
})
}
Thank you for any kind of help.