Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play Framework 2 Scala WS Doing a Sync Request

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

I need to do a sync WS request and get Result Object from it somewhere in my code.

While I was back in Java, I used to do it like this:

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

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

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

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

But I can't get Response from Future[Response] syncly! There is no .get() method on scala.

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

like image 497
Umut Benzer Avatar asked Dec 21 '22 06:12

Umut Benzer


2 Answers

Use Await.result.

import scala.concurrent.duration._
import scala.concurrent.Await

....

val future: Future[Response] = ...
Await.result(future, 10 seconds): Response
like image 94
Samy Dindane Avatar answered Dec 30 '22 10:12

Samy Dindane


Use .map and return an asynchronous result. Check out this example:

https://github.com/TechEmpower/FrameworkBenchmarks/blob/master/play-scala/app/controllers/Application.scala#L44

like image 28
Christopher Hunt Avatar answered Dec 30 '22 12:12

Christopher Hunt