2
votes

I have this code, where I am making an ask request in Scala:

 implicit val result =system.actorOf(Props(new TestActor("TestingName")),name = "Scala")

   val future3:Future[String]= ask(result ,Message).mapTo[String]
  val results = Await.result(future3, 5 second)

here my import statements

import scala.concurrent.{Await, Future}
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer
import akka.util.Timeout
import akka.pattern.ask
import scala.concurrent.Await
import scala.concurrent.duration._

and I get the error messages like

Error:(42, 35) could not find implicit value for parameter timeout: akka.util.Timeout val future3:Future[String]= ask(result ,Message).mapTo[String]

Error:(42, 35) not enough arguments for method ask: (implicit timeout: akka.util.Timeout)scala.concurrent.Future[Any]. Unspecified value parameter timeout. val future3:Future[String]= ask(result ,Message).mapTo[String]

1

1 Answers

2
votes

If you look at the signature for ask, or ?, you see:

def ask(message: Any)(implicit timeout: Timeout, 
                      sender: ActorRef = Actor.noSender): Future[Any]

It takes an implicit timeout: Timeout parameter which tells the future when to time out. You have to have it in scope when you call ask:

import scala.concurrent.duration._
import akka.util.Timeout

implicit val timeout: Timeout = Timeout(5 seconds)
val future3: Future[String]= ask(result, Message).mapTo[String]