Last active
April 30, 2019 12:54
-
-
Save hmlongco/0d2ffd86eb7d18747463c485146403ab to your computer and use it in GitHub Desktop.
FlatMapping Multiple Asynchronous API calls using Swift 5's new Result type
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
func simulateAnotherCall(_ param: String?) -> Result<String?, NWError> { | |
return makeAPICall() | |
} | |
func load() { | |
DispatchQueue.global(qos: .utility).async { | |
// make first api call and flatMap second and third api calls | |
let result = self.apiTest() | |
.flatMap { self.simulateAnotherCall($0) } | |
.flatMap { self.simulateAnotherCall($0) } | |
// handle result | |
DispatchQueue.main.async { | |
switch result { | |
case let .success(data): | |
print(data) | |
case let .failure(error): | |
print(error) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment