Google distance matrix, wait for callback to continue loop

0

I researched a lot about it and still could not find a solution.

When I run the code below, the loop proceeds before the API callback.

In the callback the variable this.userRoute.newRoute [i] .address is changed, so I need the loop to wait for the callback, to proceed from the new value that was entered in the array, which before the callback does not exist returns "undefined".

My question is if I can get the loop to wait for the callback to be executed to continue execution.

for(let i = 0; i < this.userRoute.route.length; i++){      
  service.getDistanceMatrix(
    {
      origins: [this.userRoute.newRoute[i].address],
      destinations: composeDestination,
      travelMode: 'DRIVING',
    }, this.calcRouteDistanceResult.bind(this)
  );
}
    
asked by anonymous 04.05.2018 / 17:35

1 answer

0

Actually I discovered that I do not have to do this within a loop.

The API itself allows you to provide multiple sources, and returns the distance from each source to each of the destinations, then just try json.

Eg: Source A = >     Destination A = > 2km     Target B = > 4km Origin B = >     Destination A = > 2km     Target B = > 4km

    
04.05.2018 / 19:01