-
Notifications
You must be signed in to change notification settings - Fork 638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question abount flatMapThrowing #1559
Comments
It depends on the view point. Try implementing
Lots of people get confused by this, if I could choose again, I'd probably go for something like
Functions that have this prototype:
That means a user needs to look for errors in two different places. It's usually hard enough to get error handling right if errors happen in only one way. Therefore, I don't think such functions should exist which is why NIO doesn't offer a I'm pretty sure you'd also be confused seeing a function that looks like: There's very little reason to do so: Usually you want to use either If you have a specialised application where that would make sense, feel free to add your own function |
I was thinking very same thing when trying Vapor 3 (There were tons of that). But that helped making code simpler, so I decided to use it.
I have some code like below: return fetchDB().flatMapMaybeThrowing { row -> EventLoopFuture<Void> in
let result = try handleFileSynchronously(row)
return row.updateDB(with: result)
}Synchronous func appears in asynchronous context. |
You could add your own version
This should do it extension EventLoopFuture {
func flatMapThrowingMaybe<NewValue>(_ body: @escaping (Value) throws -> EventLoopFuture<NewValue>) -> EventLoopFuture<NewValue> {
return self.flatMapThrowing { (value: Value) throws -> EventLoopFuture<NewValue> in
try body(value) // This synchronously returns a future or fails
}.flatMap {
$0 // this now continues with the result of the future (unless we failed above)
}
}
}As you can see, this is essentially a two step process:
|
Unfortunately I was using
I know the way to define |
Well, if your function is fully synchronous (does not return a future), then If you have a |
|
I'll rewrite all my |
ELF has
map/flatMap/flatMapThrowing.I wonder:
flatMapThrowingbehaves likemaprather thanflatMap?flatMap?There are some people confused like me.
flatMapThrowingis introduced in #760.The author @weissi said:
#760 (comment)
But he renamed it in that PR and I counldn't find further discussion.
I'd like to know how current design was decided on.
The text was updated successfully, but these errors were encountered: