Ignoring the result of Effects

Hi, I've run into a situation that I'd like some opinions on.

I have some network requests that I'd like to ignore the result of, but I feel that using the fireAndForget
Effect makes my API too inconsistent. I'll give some examples.

I have two network requests, let's call them start and stop. I don't do anything with the result of the stop request, so this is what I'm doing so far.

enum MyAction {
    case start
    case startResult(Result<MyData, MyError>)
    case stop

struct MyEnvironment {
    let start: () -> AnyPublisher<MyData, MyError>
    let stop: () -> Void

and then my reducer handles it like this:

case .start:
    return environment.start().catchToEffect().map(MyAction.startResult)
case .stop:
    return .fireAndForget { environment.stop() }

I don't like it very much because it makes my API inconsistent, with a mixture of return types

I'd like to be able to do something like this, where I can ignore the effect of any types of publishers:

case stop:
    return environment.stop().eraseToEffect().forget()

Do you think what I want makes sense or is it already implemented and I didn't know?

Hey Daniel,

this is actually already implemented, and looks quite similar to what you have sketched out. The only change you need to make is to have your stop effect return a publisher, and since it doesn't emit any values of interest you can do:

stop: () -> Effect<Never, Never>

And then in your reducer you can use the .fireAndForget() method on Effect to upcast its output to the type that needs to be returned:

case stop:
  return environment.stop().fireAndForget()
1 Like

Oh wow, thank you for the fast response.

I was trying to type that but I guess since my effect wasn't of type <Never, Never> it wasn't autocompleting for me.

That's great :smiley:

Hi @mbrandonw
I'm in a similar situation here. I have an effect that performs some work but doesn't return anything useful.

Effect<Void, Never>.fireAndForget {
    let url = ...
    UIApplication.shared.open(url, options: [:], completionHandler: nil)

I would like to use the same kind of api on the reducers as in:

return environment

But of course that doesn't work because the Output type of the effect is not Never but Void. But I'm wondering, should that effect have a Never output? I mean technically it does return, it's just something I don't care about. I don't fully see how the semantics of Never can apply here.

To be clear, defining the effect with Never just works but I'm wondering if we're reflecting the proper type semantics here.

@Alejandro_Martinez As long as an effect does not feed data back into the reducer it should definitely be of Output == Never. You can think of that Never meaning that this effect can never communicate what it does back to the reducer.

That makes sense. Thank you Stephen.

Terms of Service

Privacy Policy

Cookie Policy