Skip to content

Instantly share code, notes, and snippets.

@hmlongco
Last active April 30, 2019 12:54
Show Gist options
  • Save hmlongco/0d2ffd86eb7d18747463c485146403ab to your computer and use it in GitHub Desktop.
Save hmlongco/0d2ffd86eb7d18747463c485146403ab to your computer and use it in GitHub Desktop.
FlatMapping Multiple Asynchronous API calls using Swift 5's new Result type
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