我正在使用calculate()函数从google-maps-api获取数据。在此函数中,我将一些数据追加到数组,但这需要时间。而且,当我在其他函数中使用此函数时,首先无法运行calculate()。如何在sortTravel函数中执行内联函数?
const calculate = (arr) =>{
for(let i = 0; i < arr.length;i++){
let request = {
origin : arr[i].from, // initializing values of
destination : arr[i].to,
travelMode: google.maps.TravelMode.DRIVING, //WALKING, BYCYCLING, TRANSIT
unitSystem: google.maps.UnitSystem.METRIC
}
directionsService.route(request, (result, status) => {
if(status == google.maps.DirectionsStatus.OK){
arr[i]['value'] = result.routes[0].legs[0].distance.value;
}
});
}
return arr;
}
const sortTravel = (array) => {
let travel = [];
while(array.length > 1){
let positionedArr = setPosition(array);
let valued = calculate(positionedArr); //add distance values and combine adrresses
console.log(valued);
let min = getMinObject(valued);
//console.log(positionedArr.value);
array = dltElement(array,min.from);
let to = min.to;
array = dltElement(array,min.to);
array.unshift(to);
travel.push(getMinObject(valued));
}
return travel; }