title: Synchronous request using Alamofire
body: <p>I have checked other solutions like <a href="https://stackoverflow.com/questions/39961010/how-to-make-a-synchronous-request-using-alamofire">How to make a synchronous request using Alamofire?</a>, I followed all instructions but I haven't been able to complete the requests and then use the information.</p>
  
  <p>I need to retrieve distance and route points from google maps and then complete an array with that information.
  This is the function:</p>
  
  <pre><code>func getGoogleMapsInfo(startLocation: CLLocationCoordinate2D, restaurant: Customer, completion: @escaping (_ distance: Int, _ routePoints: String)  -&gt; ()){
  
      let endLocation = CLLocationCoordinate2D(latitude: restaurant.latitude, longitude: restaurant.longitude)
      let origin = "\(startLocation.latitude),\(startLocation.longitude)"
      let destination = "\(endLocation.latitude),\(endLocation.longitude)"
      var distance = 0
      var routePoints = ""
  
      let url = "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin)&amp;destination=\(destination)&amp;mode=walking"
      Alamofire.request(url).responseJSON { response in
  
          switch response.result {
          case .success:
  
              do {
                  self.json = try JSON(data: response.data!)
  
                  distance = Int(truncating: self.json["routes"][0]["legs"][0]["distance"]["value"].numberValue)
  
  
                  let route = self.json["routes"].arrayValue.first
                  let routeOverviewPolyline = route!["overview_polyline"].dictionary
                  routePoints = (routeOverviewPolyline?["points"]?.stringValue)!
                  completion(distance, routePoints)
                  break
              } catch {
                  print("error JSON")
              }
  
  
          case .failure(let error):
              print(error)
              completion(distance, routePoints)
              break
          }
  
      }
  
  
  }
  </code></pre>
  
  <p>And this is how I call it:</p>
  
  <pre><code>    for index in 0...nearbyRestaurants.count - 1 {
  
          getGoogleMapsInfo(startLocation: currentLocation, restaurant: nearbyRestaurants[index]) { (distance, routePoints) in
              nearbyRestaurants[index].distance = distance
              nearbyRestaurants[index].routePoints = routePoints
          }
  
      } 
  </code></pre>
  
  <p>I'd really appreciate if someone can help me.</p>