title: How to use DispatchSemaphore with Alamofire and SwiftyJSON?
body: <p>I was trying to use some json response outside the method but it returns empty array when method is executed, but inside the block works as expected, is there any way my method will return expected value, here is my sample code:</p>
  
  <pre><code>func getCarouselContent(url: String) -&gt; [String] {
      var ids: [String] = []
      let headers = ["api_key": "My_API_KEY"]
  
      let semaphore = DispatchSemaphore(value: 0)
      Alamofire.request(url, headers: headers).validate().responseJSON { 
          (response) in
          semaphore.signal()
          switch response.result {
          case .success(let value):
              let json = JSON(value)
              let data = json["data"]["relationships"]["slides"]["data"]
              for child in data.array! {
                  let id = child["id"].string!
                  print(id) // this prints all the id
                  ids.append(id)
              }
          case .failure(let error):
              print(error)
          }
      }
      semaphore.wait()
      return ids 
  }
  </code></pre>
  
  <p>I am using alamofire and swiftyjson to parse the json. FYI I am new to this and tried solutions from the response of similar questions but didn't work, any advice is highly appreciated, Thanks.</p>