I've got a interesting situation here which I'm trying to understand which is the best way to write this code.
I've got a service which I use to inject in a component to fetch data from the server which returns an observable. Here is the code :
getDataFromServer(): Observable<any> {
this.storage.get('access_token').then((access_token) => {
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded', 'Authorization': 'Bearer ' + access_token });
let options = new RequestOptions({ headers: headers });
});
return this.http.get(config.apiArticlesUrl, options)
.map(this.extractData)
.catch(this.handleError);
}
So as you can see here this method is expected to return Observable. It also has a storage method being executed inside the method body which returns a promise with resulting data needed to fetch the data from the server using a Bearer token.
Now, Typescript complains that in this case the options variable is being undefined at this point because of scoping issue. If I defined options variable outside the .then(function) body then all the time options variable will be empty because of the promise execution and I can authenticate to the server because of that.
If I put return this.http.get(config.apiArticlesUrl, options).map(this.extractData).catch(this.handleError); inside the .then(function{}) body then TypeScript will complain that Observable needs to return from this method.
The important part is that I'm using import { Storage } from '@ionic/storage'; which whenever you use .get method it always returns a Promise which is not a good practice to change 3rd party core code.
The question is : Which is the best way to write this code so whenever I subscribe to the Observable I will get the user authenticated combining Promise and Observable?
I hope I'm asking the question in the right format and if it's possible to share some link where this is all explained in a clean way that would be very useful.
Cheers.