returning no effect in an effect mapping #1068
-
|
we have this pattern where we frequently create a we can't just return nil because that's an invalid mapping. this means we have to add a |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
|
Undoubtedly there are other practices, but our pattern is to have an apiCallFinished action that accepts the return environment.dataClient
.apiCall()
.receive(on: environment.mainQueue)
.catchToEffect(Action.apiCallFinished)
.cancellable(id: APICallId(), cancelInFlight: true)
case let .apiCallFinished(.success(response)):
// if some condition
return .none
case let . apiCallFinished(.failure(error)):
// handle error
return .none |
Beta Was this translation helpful? Give feedback.
-
Instead of |
Beta Was this translation helpful? Give feedback.
-
|
I’d recommend the approach in the post of above and handle this logic in your reducer, but you can also use flatMap and conditionally return an Effect(value:) or Effect.none. |
Beta Was this translation helpful? Give feedback.
Instead of
map, you can use Combine'scompactMap, which lets you filter outniloutputs in the closure. Just make sure you call.eraseToEffect()on the publisher it returns to get things back in anEffect.