Play Framework 2 Scala WS Executing a Sync Request

I am learning Scala. I used to use the Play Framework 2 Java and tried to rewrite some of my previous work using and learning Scala.

I need to execute a WS synchronization request and get the result object from it somewhere in my code.

While I was in Java, I did it like this:

WS.url("someurl").get().get(5000); 

or with T Promise<T>.get(Long timeout) , to be precise.

Since I switched to Scala, now I use play.api.libs.ws and I rewrote the code as:

 val somefuture:Future[Response] = WS.url("someurl").get(); 

But I can’t get the Answer from the Future [Answer] synchronously! Scala does not have a .get() method.

How can I get a Response object from a Future[Response] syncly?

+4
source share
2 answers

Use Await.result .

 import scala.concurrent.duration._ import scala.concurrent.Await .... val future: Future[Response] = ... Await.result(future, 10 seconds): Response 
+9
source

All Articles