0

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');
Marcatectura
  • 1,721
  • 5
  • 30
  • 49
  • `await` only works with asynchronous functions if they return a promise. See the question yours is marked a duplicate of for how to return a result from an asynchronous operation. – jfriend00 Jan 14 '18 at 05:33
  • I understand that. I thought by making them async functions, their return values would implicitly be Promises. This question is not a duplicate of the one you indicated. The accepted answer for that question explains the components of promises, async...await, etc and how they're used. My issue is that I am attempting to incorporate the async...await approach, but I obviously am misunderstanding something and am hoping someone can help clarify. Please re-open this question. – Marcatectura Jan 14 '18 at 05:45
  • You're trying to return a value from an asynchronous operation. That answer tells you how to do it. There are several options there, including ones with promises. Yes, an async function always returns a promise, but it's your `getAddress()` function that's broken because the returned promise has no connection whatsoever to the `googleMapsClient.geocode()` function and the solution to that is in that other answer. You don't need `async` here. It doesn't help you at all. If you want to use promises, then you have to promisify `googleMapsClient.geocode()` first and just return that promise. – jfriend00 Jan 14 '18 at 05:55
  • You can only `await` an asynchronous operation that returns a promise that is properly resolved when the underlying async operation is done. You are not doing that last part and that other answer shows you how to do that. Then, you can `await` it if you want, but not until you make `getAddress()` return a promise that is resolved or rejected when `googleMapsClient.geocode()` is done. – jfriend00 Jan 14 '18 at 05:59
  • This article might help you: [Introduction to Google Maps API Featuring ES6 Promises](https://jamesbedont.com/2016/03/23/GoogleMapsApiIntroduction.html#geocode) – jfriend00 Jan 14 '18 at 06:02
  • Thanks, I'll work on converting the function with promisify. – Marcatectura Jan 14 '18 at 06:11
  • Here's another one that might help: [Promises With Google Maps Geocoder API](https://stackoverflow.com/questions/46064244/promises-with-google-maps-geocoder-api) – jfriend00 Jan 14 '18 at 06:14

0 Answers0