title: How to flatten multiple queries in Vapor
body: <p>I need to do lots of queries to different tables and return the results in one single response. I'm wondering ow could I flatten the extremely nested function?</p>
  
  <pre><code>final class CodeController {
      internal func indexCodes(_ req: Request) throws -&gt; EventLoopFuture&lt;Response&gt; {
          CodeCountry.query(on: req).sort(\.name).all().flatMap { countries -&gt; EventLoopFuture&lt;Response&gt; in
              CodeFloor.query(on: req).sort(\.name).all().flatMap { floors -&gt; EventLoopFuture&lt;Response&gt; in
                  CodeRegion.query(on: req).sort(\.name).all().flatMap { regions -&gt; EventLoopFuture&lt;Response&gt; in
                      CodeObjectType.query(on: req).sort(\.name).all().flatMap { objectTypes -&gt; EventLoopFuture&lt;Response&gt; in
                          CodePropertyType.query(on: req).sort(\.name).all().flatMap { propertyTypes -&gt; EventLoopFuture&lt;Response&gt; in
                              CodeRooms.query(on: req).sort(\.name).all().flatMap { rooms -&gt; EventLoopFuture&lt;Response&gt; in
                                  let codes = CodesContent(
                                      countries: countries,
                                      floors: floors,
                                      regions: regions,
                                      objectTypes: objectTypes,
                                      propertyTypes: propertyTypes,
                                      rooms: rooms
                                  )
  
                                  return codes.encode(status: .ok, for: req)
                              }
                          }
                      }
                  }
              }
          }
      }
  }
  </code></pre>