Konstantin Bodnia
Konstantin Bodnia

Reputation: 1513

Akka Streams: How to update one field with the result of the future

I have an entity passing down the akka stream and it has one field that has to be updated during one of the flows.

Let's say case class Entity(f: Int)

The value to update the entity is coming from the future.

Flow[Entity]
  .map({ entity  ⇒
    entity.copy(
      f = // get result of the future
    )
  })

There are several options coming to my mind.

First is to Await for the execution of the future. But in this case, I would have to provide it with its own execution context etc... How do I use the graph's execution context within a flow?

Second is passing a tuple of (Entity, Future[Int]) to the next stage. But it would be easier to transform it into Future[(Entity, Int)] and then mapAsync it. But is there a way to transform a Tuple with Future into a Future of a Tuple within akka stream?

What would be a perfect solution to this simple problem?

Upvotes: 0

Views: 174

Answers (1)

Mateusz Kubuszok
Mateusz Kubuszok

Reputation: 27535

How about something like:

Flow[Entity]
  .mapAsync { entity =>
    createIntFuture.map { int =>
      entity.copy(f = int)
    }
  }

?

Upvotes: 2

Related Questions