Skip to content

Instantly share code, notes, and snippets.

@Thomvis
Created November 26, 2015 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Thomvis/dc9cae1ff295dc7176b6 to your computer and use it in GitHub Desktop.
Save Thomvis/dc9cae1ff295dc7176b6 to your computer and use it in GitHub Desktop.
An extension of NSURLSession showcasing how its functionality could be wrapped in Futures
extension NSURLSession {
func fetch(url: String) -> Async<Result<(NSData, NSURLResponse), Error>> {
return Async { completion in
let task = self.dataTaskWithURL(NSURL(string: url)!, completionHandler: { (data, response, error) -> Void in
if let data = data, response = response {
completion(Result(value: (data, response)))
} else {
completion(Result(error: .LegacyError(error)))
}
})
task.resume()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment