4

I'm using the Google Maps Geocode API and attempting to use async await. I've defined several functions for handling the request:

function googleGeoCode(address) {
  const googleMapsClient = require('@google/maps').createClient({
    key: 'googleMapsApiKeyGoesHere',
    Promise: Promise
  });

  return googleMapsClient.geocode({ address: address }).asPromise();
}

async function getGeoCode(address, errors, res) {
  try {
    const result = await googleGeoCode(address);
    return result;
  } catch (error) {
    errors.googleMapsClient = error;
    return res.status(400).json(errors);
  }
}

I then use the getGeoCode function in my express route:

const geoResponse = getGeoCode(req.body.address, errors, res);

The await portion of the function is not working correctly. If i console log geoResponse I get Promise { <pending> }

I'm new to using async await and I'm not sure if I am doing something incorrect here. Any help is really appreciated!

1 Answer 1

2

An async function always returns a promise or wraps the return value into promise,you have to resolve the promise like this

 const geoResponse = getGeoCode(req.body.address, errors, res);

    geoResponse.then((result)=>{

    console.log(result)
    }).catch((err)=>{
     console.log(err);
    })
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.