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:

Terms of Service

Privacy Policy

Cookie Policy