I've been trying to write a program which ultimately will take a set of distances and input them into an array. Through the code I've attached, I found the distance between 2 locations, and I can put that distance into the console log— I just can't return it in that function. I've tried using async/await and Promises to maybe try and get it out of the function, but that results in the array containing fulfilled promises, however the data is "undefined" (in the code below, I removed async await implementation). Please advise on how I can return this distance value and eventually put it into an array.
function getDistances(directionsService, destination){
var route = {origin: document.getElementById("Address").value, // User input for their address
destination: destination,
travelMode: 'DRIVING'}
directionsService.route(route, // Request for a route
function(response, status) {
if (status !== 'OK') {
window.alert('Directions request failed due to status error: ' + status);
return;
}
else {
var directionsData = response.routes[0].legs[0]; // Get data about the mapped route
if (!directionsData) {
window.alert('Directions request failed');
return;
}
else {
var currentDist = directionsData.distance.value;
return currentDist; // When this is returned, it's always undefined
}
function getDistances(directionsService, destination){