title: Multiple Future calls in an Actor's receive method
body: <p>I'm trying to make two external calls (to a Redis database) inside an Actor's <code>receive</code> method. Both calls return a <code>Future</code> and I need the result of the first <code>Future</code> inside the second. 
  I'm wrapping both calls inside a Redis transaction to avoid anyone else from modifying the value in the database while I'm reading it.  </p>
  
  <p>The internal state of the actor is updated based on the value of the second <code>Future</code>. </p>
  
  <p>Here is what my current code looks like which I is incorrect because I'm updating the internal state of the actor inside a <code>Future.onComplete</code> callback. </p>
  
  <p>I cannot use the <code>PipeTo</code> pattern because I need both both Future have to be in a transaction. 
  If I use <code>Await</code> for the first <code>Future</code> then my receive method will <em>block</em>. 
  Any idea how to fix this ? </p>
  
  <p>My <strong>second question</strong> is related to how I'm using <code>Future</code>s. Is this usage of <code>Future</code>s below correct? Is there a better way of dealing with multiple Futures in general? Imagine if there were 3 or 4 Future each depending on the previous one.  </p>
  
  <pre><code>import akka.actor.{Props, ActorLogging, Actor}
  import akka.util.ByteString
  import redis.RedisClient
  
  import scala.concurrent.Future
  import scala.util.{Failure, Success}
  
  
  object GetSubscriptionsDemo extends App {
    val akkaSystem = akka.actor.ActorSystem("redis-demo")
    val actor = akkaSystem.actorOf(Props(new SimpleRedisActor("localhost", "dummyzset")), name = "simpleactor")
    actor ! UpdateState
  }
  
  case object UpdateState
  
  class SimpleRedisActor(ip: String, key: String) extends Actor with ActorLogging {
  
    //mutable state that is updated on a periodic basis
    var mutableState: Set[String] = Set.empty
  
    //required by Future
    implicit val ctx = context dispatcher
  
    var rClient = RedisClient(ip)(context.system)
  
    def receive = {
      case UpdateState =&gt; {
        log.info("Start of UpdateState ...")
  
        val tran = rClient.transaction()
  
        val zf: Future[Long] = tran.zcard(key)  //FIRST Future 
        zf.onComplete {
  
          case Success(z) =&gt; {
            //SECOND Future, depends on result of FIRST Future 
            val rf: Future[Seq[ByteString]] = tran.zrange(key, z - 1, z) 
            rf.onComplete {
              case Success(x) =&gt; {
                //convert ByteString to UTF8 String
                val v = x.map(_.utf8String)
                log.info(s"Updating state with $v ")
                //update actor's internal state inside callback for a Future
                //IS THIS CORRECT ?
                mutableState ++ v
              }
              case Failure(e) =&gt; {
                log.warning("ZRANGE future failed ...", e)
              }
            }
          }
          case Failure(f) =&gt; log.warning("ZCARD future failed ...", f)
        }
        tran.exec()
  
      }
    }
  
  }
  </code></pre>
  
  <p>The compiles but when I run it gets struck.</p>
  
  <pre><code>2014-08-07  INFO [redis-demo-akka.actor.default-dispatcher-3] a.e.s.Slf4jLogger - Slf4jLogger started
  2014-08-07 04:38:35.106UTC INFO [redis-demo-akka.actor.default-dispatcher-3] e.c.s.e.a.g.SimpleRedisActor - Start of UpdateState ...
  2014-08-07 04:38:35.134UTC INFO [redis-demo-akka.actor.default-dispatcher-8] r.a.RedisClientActor - Connect to localhost/127.0.0.1:6379
  2014-08-07 04:38:35.172UTC INFO [redis-demo-akka.actor.default-dispatcher-4] r.a.RedisClientActor - Connected to localhost/127.0.0.1:6379
  </code></pre>
  
  <p><strong>UPDATE 1</strong></p>
  
  <p>In order to use <code>pipeTo</code> pattern I'll need access to the <code>tran</code> and the FIRST Future  (<code>zf</code>) in the actor where I'm piping the <code>Future</code> to because the SECOND <code>Future</code> depends on the value (<code>z</code>) of FIRST. </p>
  
  <pre><code>    //SECOND Future, depends on result of FIRST Future 
        val rf: Future[Seq[ByteString]] = tran.zrange(key, z - 1, z) 
  </code></pre>