title: Function does not wait until the data is downloaded
body: <p>I have the following function which downloads an image from server; </p>
  
  <pre><code>func getImageFromServerById(imageId: String) -&gt; UIImage? {
      let url:String = "https://dummyUrl.com/\(imageId).jpg"
      var resultInNSDataformat: NSData!
  
      let task = NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: url)!) {(data, response, error) in
          if (error == nil){
              resultInNSDataformat = data
          }
      }
      task.resume()
      return UIImage(data: resultInNSDataformat)
  }
  </code></pre>
  
  <p>The function does not wait for the download task to be completed before returning the image. Therefore my app always crashes. Any ideas for how to wait for the download?</p>