How to implement a 'fanout' pattern - N number of side effects

I’m trying to avoid doing side effects inside the action controller method, but am struggling to figure out how to send a sequence of futures. The doc shows example of composing calls using a for comprehension that returns an arbitrary response when all steps completed, but not seeing how I can apply that to collection of futures of arbitrary size.

My naive implementation:

What I’m trying to do to improve it:

Thanks in advance.

Hi @maristi I’m not sure if I fully understand your problem but Future.sequence is fine, you just need to compose it with asyncReply.

    val f1: Future[String] = ???
    val f2: Future[Int] = ???
    effects.asyncEffect(Future.sequence(List(f1, f2)).map(_ => effects.reply(Empty.defaultInstance)))

Make sure to add some error handling in more advanced solution.

1 Like