In attempting to wrap a Google Maps API call in an async
function, I'm unable to get the response logged in the console - is there something wrong with how I'm attempting to access the value returned by the maps API function? My code consists of two async processes which should run sequentially, then a function which deals with both processes. The idea is to supply a query string with a location, the Maps API will look it up, then return the formatted address in the console.
Google Maps process:
const mapsApiKey = 'API_KEY';
const googleMapsClient = require('@google/maps').createClient({
key: mapsApiKey
});
async function getAddress(queryString) {
googleMapsClient.geocode({
address: queryString
}, function (err, response) {
if (!err) {
let formattedAddress = response.json.results[0]['formatted_address'];
return formattedAddress;
} else {
return err;
}
});
}
Console.log process:
async function showResult(address) {
return("result from Promise: " + getAddress());
}
Main async function:
async function handleAddress(inputString) { // originally async function endpointHandler(request, response) {
try {
let awaitedResult = await getAddress(inputString);// function we want as var, with params
let waitingFunc = await showResult(formattedAddress);// should only executes after awaitedResult gets resolved!!!
console.log(waitingFunc());
} catch(err) {
// handle errors here
}
}
handleAddress('London');