0

I am trying to return an API's object so it is usable in the global scope or inside the geolocation.navigator function scope of my main script file. I have a separate module for my ajax request and am importing it to my main script file.

app-functions.js:

function reverseGeocode(lat, lng) {
    return $.ajax({
        url: "libs/php/reverse-geocode.php",
        type: 'POST',
        dataType: 'json',
        data: {
            lat: lat,
            lng: lng
        },
        success: function(data){

            return data;
        }
    });
};

export { reverseGeocode };

When I call the ajax function I use the .then() function to return and console.log the result, but this only has block scope. I have tried to return it again but it displays as undefined in the global scope.

script.js:

import { reverseGeocode } from './app-functions.js';

navigator.geolocation.getCurrentPosition(function showPosition(position) {

        lat = position.coords.latitude;
        lng = position.coords.longitude;

        marker = L.marker([lat, lng], {icon: myIcon1}).addTo(mymap);

        reverseGeocode(lat, lng).then(function(result) {
        console.log(result);
        return result
        });
        
        console.log(result); // Returns undefined
});
Jed Boyle
  • 13
  • 5

0 Answers0