title: Recursive Function, With Completion Block, That Retrieves Multiple MKDirections - Swift
body: <p>I'm trying to retrieve an array of <code>MKRoute</code> that contains multiple routes all starting at the same place, but each having a different destination.</p>
  
  <p>The problem is that the only way I've been able to come up with to do this is through a recursive function, but I can't find anything on how to use recursive functions with completion blocks. Since loading the routes is done Asynchronously a completion block is needed.</p>
  
  <p>How do I go about getting the following functionality but with completion blocks? The "add to the return" functionality?</p>
  
  <pre><code>func factorial(of num: Int) -&gt; Int {
      if num == 1 {
          return 1
      } else {
          return num * factorial(of:num - 1)
      }
  }
  </code></pre>
  
  <p>Here is my function code</p>
  
  <pre><code>func getDirections(originCoordinate: CLLocationCoordinate2D, destinationCoordinates: [CLLocationCoordinate2D], completion: @escaping(_ routes:[MKRoute]?, _ error: Error?) -&gt; Void) {
  
      // Origin is the same for each route, what changes is the destination
      // A singular origin coordinate with an array of destination coordinates is passed in
      // The function would in theory be recursive, returning each route from the origin to each of the destinations.
  
      // Leave function if no more destination coordinates are passed
      if destinationCoordinates.count == 0 {return}
  
      // Origin
      let originPlacemark = MKPlacemark(coordinate: originCoordinate)
      let originItem = MKMapItem(placemark: originPlacemark)
  
      // Destination is made from the first element of the passed in destinationCoordinates array.
      let destinationPlacemark = MKPlacemark(coordinate: destinationCoordinates.first!)
      let destinationItem = MKMapItem(placemark: destinationPlacemark)
  
      // Direction Request setup
      let directionRequest = MKDirections.Request()
      directionRequest.source = originItem
      directionRequest.transportType = .automobile
      directionRequest.destination = destinationItem
  
      let directions = MKDirections(request: directionRequest)
  
      // Calculating directions
      // Heart of function
      directions.calculate { (response, err) in
  
          // Checking if a response is returned
          guard let response = response else {
              completion(nil, err)
              return
          }
  
          // Response is returned
          let route = response.routes[0]
  
          let tail = Array.dropFirst(destinationCoordinates)
  
          // Recursive part that I'm not sure how to properly implement
          completion([route].append(getDirections(originCoordinate, tail)), nil)
  
      }
      // If no response is retrieved, our guard let statement sends us here
  }
  </code></pre>