I would like to write a function that returns data from the google places API in node.js. The function is defined as seen below:
function getAddress(placeName) {
return new Promise((resolve, reject) => {
return axios.get('https://maps.googleapis.com/maps/api/geocode/json?address=' + placeName + '&key=MyAPIKey').then(response => {
const placeID = response.data.results[0].place_id
return axios.get('https://maps.googleapis.com/maps/api/place/details/json?placeid=' + placeID + '&key=MyAPIKey').then(response => {
resolve(response.data.result)
return response.data.result // here i would like to return the data.result
}).catch(err => {
console.error(err);
});
}).catch(err => {
reject(err);
});
});
}
I want to use the function and get a return value. I have tried with the code below, but i get the error 'Promise pending'
const address = getAddress('someName').then(address => {
phone: address.formatted_phone_number
}).catch(err => {
console.error(err)
})
So how do i construct the function, so it returns the data?