when this code is written
var holder = [];
const compile = () =>{
let latitude = 0;
let longitude = 0;
for (let i = 0; i < holder.length; i++) {
Geocode.fromAddress(holder[i].city).then(
(response) => {
const { lat, lng } = response.results[0].geometry.location;
const foo = {lat, lng}
longitude += Number(foo.lng);
console.log(longitude)
latitude += Number(foo.lat);
console.log(latitude)
},
(error) => {
console.error(error);
}
);
}
console.log("lat: " + latitude)
latitude = latitude/holder.length;
longitude = longitude/holder.length;
console.log("lattitude: ", latitude, " longitude: ", longitude);
}
the output looks like this
lat: 0 AddBox.js:49
lattitude: 0 longitude: 0 AddBox.js:53
-118.2436849 AddBox.js:39
34.0522342 AddBox.js:41
-192.7821738 AddBox.js:39
74.62067049999999
why would the console.log be printing before the for loops runs?
I'm trying to average out the longitudes and latitudes of several locations using geocode, but the code doesn't seem to be running from top to bottom