Mads Lundeland
Mads Lundeland

Reputation: 95

Nested futures in Akka

I have two actors, A and B. My pseudo code would read like this:

if A has a given state
    return "ok"
else 
    send a message to B and return "ok" when B is done handling the message

This is my implementation using Await:

val f1 = (A ? GetState).mapTo[Option[State]]
f1.map {
    case Some(state) => "OK"
    case None =>
        val f2 = B ? Process
        Await.result(f2, 1 seconds) // todo: get rid of this
        "OK"    
}

I can not figure out how this could be implemented without the Await. Anyone?

Upvotes: 0

Views: 463

Answers (1)

cmbaxter
cmbaxter

Reputation: 35443

Try the code below and see if it works for you:

val f1 = (A ? GetState).mapTo[Option[State]]
f1.flatMap {
    case Some(state) => Future.successful("OK")
    case None =>
        val f2 = B ? Process
        f2.map(t =>  "OK")    
}

Upvotes: 3

Related Questions