I have a situation where I need to fetch a piece of data from storage in an Ionic 2 application and then use that data to create an HTTP request. The problem that I am running i
In angular 2, the Http
service functions (get
, post
, etc.) return an Observable object. This is just the way they implemented it.
If you're used to promises, and want your service to return a promise instead, you can use the toPromise
function that's built in Observable
objects.
loadStuff(){
return this.tokenService.getToken().then(token => {
return this.http.get("https://www.urltodatasource.com/api/v1/Endpoint?access_token="+token).toPromise());
});
}
And then
this.tokenService.loadStuff().then(data => {
data = data.json(); //you might need to do that, depending on the structure of the response
this.storage.set('stuff', data);
return data;
});