Skip to content

Instantly share code, notes, and snippets.

@zhxnlai
Last active June 15, 2016 19:18
Show Gist options
  • Save zhxnlai/995620226b21d41407b2a0e9b138683d to your computer and use it in GitHub Desktop.
Save zhxnlai/995620226b21d41407b2a0e9b138683d to your computer and use it in GitHub Desktop.
Async Task NSURL
extension NSURL : ThrowableTaskType {
public typealias ReturnType = (NSData, NSURLResponse)
public func action(completion: Result<ReturnType> -> ()) {
ThrowableTask<ReturnType> {
let session = NSURLSession(configuration: .ephemeralSessionConfiguration())
let (data, response, error) = Task { callback in session.dataTaskWithURL(self, completionHandler: callback).resume()}.await()
guard error == nil else { throw error! }
return (data!, response!)
}.asyncResult(completion: completion)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment