Created
November 26, 2015 20:16
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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