I didn't want to use a time interval loop since I thought there must be a more modern way to solve this. So I found another interesting solution on stackoverflow that lets me attach an event listener to a variable and execute my code once the variable is set.
https://stackoverflow.com/a/37403125/4688612
My solution now looks like this:
In my geolocation script I install a variable( latitude and longitude ) with an event listener.
geolocation = {
latlonInternal: '',
latlonListener: function(val) {},
set latlon(val) {
this.latlonInternal = val;
this.latlonListener(val);
},
get latlon() {
return this.latlonInternal;
},
registerListener: function(listener) {
this.latlonListener = listener;
}
}
In my Google Places Autocomplete API I register the listener and wait until the variable is set.
geolocation.registerListener( function(val){
console.log( 'latitude = ' + val[0]);
console.log( 'longitude = ' + val[1]);
});
Once the geolocation service has retrieved the position it will update the geolocation variable and trigger the listener.
geolocation.lonlat = [ geoipResponse.location.latitude, geoipResponse.location.longitude ];
This way my code bases for GeoIP and Google Paces Autocomplete API are completely separated and nothing needs to be nested.