Reputation: 897
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!
Upvotes: 4
Views: 4225
Reputation: 1
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);
})
Upvotes: 2