I have some nodejs code used to retrieve data from google maps API:
const mapsAPI = new Client({});
let countryData = mapsAPI.reverseGeocode( {
params: {
latlng: latlng, // there's probably a better way of doing this
key: process.env.GOOGLE_MAPS_API_KEY,
},
timeout: 1000, // in miliseconds
})
.then((response) => {
// console.log(response.data.results[0].reverseGeocode); // prints whole response
// console.log(response.data.results[7].types); // prints types 'country' and 'political'
// console.log(response.data.results[7].formatted_address); // prints country
country = response.data.results[7].formatted_address;
console.log(country);
return country;
})
.catch((e) => {
console.log(e.response.data.error_message); // NOTE: this will only return API errors
});
// i know the code is a mess, will fix later
// maybe could make this a function and just return response data
async function checkAddress() {
const a = await countryData;
// console.log(a);
if (a === 'United States') {
console.log('true');
return true;
}
else {
console.log('false');
return false;
}
return false;
}
console.log(checkAddress()); // returns Promise { <pending> } for some reason
checkAddress() should return true or false, but returns Promise { <pending> }, what am I doing wrong?
new to async await, please dont kill me