Last active
May 17, 2019 02:14
-
-
Save hsleedevelop/9832df98b28e4f6e3eee218fa6408bc3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
func makeAPICall() -> Result<String?, NetworkError> { | |
let path = "https://jsonplaceholder.typicode.com/todos/1" | |
guard let url = URL(string: path) else { | |
return .failure(.url) | |
} | |
var result: Result<String?, NetworkError>! | |
let semaphore = DispatchSemaphore(value: 0) | |
URLSession.shared.dataTask(with: url) { (data, _, _) in | |
if let data = data { | |
result = .success(String(data: data, encoding: .utf8)) | |
} else { | |
result = .failure(.server) | |
} | |
semaphore.signal() | |
}.resume() | |
_ = semaphore.wait(wallTimeout: .distantFuture) | |
// if semaphore.wait(timeout: .now() + 15) == .timedOut { | |
// result = .failure(.timeout) | |
// } | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment