If subscribe returns a Promise
, return
call
, chain .then()
to get the value this.latLong
returned from within .subscribe()
getCoords() {
let url = 'https://maps.googleapis.com/maps/api/geocode/json?address='
+ address + '&key=' + this.key;
let call = new Promise((resolve, reject) => {
this.http.get(url).subscribe(data => {
let lat = data.json().results[0].geometry.location.lat;
let long = data.json().results[0].geometry.location.lng;
this.latLong = {
"lat": lat,
"long": long
};
//I can get it here
console.log('called >> ', this.latLong)
resolve(this.latLong);
});
});
return call.then(res => { console.log(res); return res })
//this.latlong is undefined !
}
getCoords()
.then(data => console.log(data))
.catch(err => console.log(err));