Can you have an Effect return another Effect? i.e. Chain Effects together and pass one Output to another in the same Action?

I have a scenario where I have one Effect that returns an array of [String]. I then pass that to my Networking Effect which makes an API request and returns a Decodable Output, which is what I ultimately want to use to make a decision and return an Action. Is there a way to chain Effects together where you can use the Output of one Effect and pass it to another Effect, without needing to return an Action between the Effects?

If your effects are based on Combine publishers, then .flatMap operator is what you need to use. If your effects are based on async/await dependencies endpoints then just call them in Effect.run { send in } and send action in the end.

Unfortunately they are Async/Await based. Not possible using that?

If async/await then it depends it they are function or AsyncStream. If normal async function then try something it like this:

case .fetchItems:
  return .run { send in 
    let payload = await self.preparePayload()  // returns [String]
    let response = await self.apiClient.fetchFor(payload)  // returns Decodable Output
    send(.updateItems(response))
  }

If your endpoints are AsyncStreams, then await/reduce/map/flatMap them in .run { send in } as you need and in then use send(.action(finalDataReadyToBeSendToAction)).

When dealing with async/await endpoints then it's all like handling this as a normal swift concurrency in Effect.run.

1 Like

Great, that worked. Thank you!