I am new to JS, I start a ionic 2 app project. the question is simple but hard for me to understand, I have a LocationService:
getCurrentLocation(){
return this.geolocation.getCurrentPosition() //return Promise<Geoposition>
}
and then I want consume the Promise in ForecastService:
getWeather() {
this.locationService.getCurrentLocation().then((resp) => {
// resp.coords.latitude +","+ resp.coords.longitude
var loc = resp.coords.latitude + "," + resp.coords.longitude
return this.http.get(this.API_ENDPOINT + loc).map(res => res.json())
}).catch((error) => {
console.log('Error getting location', error);
})
}
the above code is wrong I think but I don't know how to return an Observable
in Promise.then
and I want bind the http.get() result in component to component variable this.data
:
getJson(){
this.ForecastService.getWeather().subsribe(res=>{ this.data = res.data})
}