I am using the Google Maps JS API to Geocode some addresses, but I need to have access to a number of variables inside the callback of the geocode function. The geocoder is an asynchronous function.
My code is as follows:
for (var prop in markerData) {
    geocoder.geocode( { address: addressData }, function(results, status) {
        if (status == 'OK') {
            lat = results[0].geometry.location.lat();
            lng = results[0].geometry.location.lng();
            console.log(addressData);
            markerInfo = { 
                lat: lat, 
                lng: lng,
                firstname: markerData[prop].firstname,
                lastname: markerData[prop].lastname,
                company: markerData[prop].company,
                addressFormatted: addressData,
                street: markerData[prop].street,
                city: markerData[prop].city,
                postcode: markerData[prop].postcode,
                telephone: markerData[prop].telephone
            }
            markers[i] = new google.maps.Marker({
                map: map,
                position: {lat: lat, lng: lng},
                title: markerData[prop].firstname + ' ' + markerData[prop].lastname,
                markerInfo: markerInfo
            });
            markers[i].addListener('click', function() {
                updateInfo(this.markerInfo);
            });
            $.get('cache.php', {
                address: addressData,
                lat: lat,
                lng: lng
            });
        }
    });
}
addressData is always the value of the last entry of markerData.  markerData data is definitely correct.
I need a way of passing both addressData and markerData to the geocoder.geocode function that will be accessible inside the callback.
 
    