2

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})
}
WesleyHsiung
  • 351
  • 2
  • 13
  • Repeated question. Answered solution [here](https://stackoverflow.com/a/41585663/2933860) – AlexisCaffa Jun 01 '18 at 00:59
  • Possible duplicate of [How to return Observable after some Promise get resolved in Ionic 2/Angular 2?](https://stackoverflow.com/questions/41585514/how-to-return-observable-after-some-promise-get-resolved-in-ionic-2-angular-2) – AlexisCaffa Jun 01 '18 at 01:00

0 Answers0