I'm using a method(FinagleClient.longRunningComputation) of Finagle client in my controller
def alfa = Action.async(parse.json) { request =>
val response = FinagleClient.longRunningComputation(request.body )
response.map( rest => Ok("Got result: " + rest ))
}
I need to get the result of the com.twitter.util.Future in my play Controller
I try using Returning futures , but the compiler, return the following error:
found : com.twitter.util.Future[play.api.mvc.Result]
required: scala.concurrent.Future[play.api.mvc.Result]
response.map(rest => Ok("Got result: " + rest))
I need to use a com.twitter.util.Future in my play Controller
How Can I Do This?
You will have to convert to scala.concurrent.Future
, which you can do using Promise
.
Something like this:
def alfa = Action.async(parse.json) { request =>
val p = Promise[Result]
val response = FinagleClient.longRunningComputation(request.body )
val twitterFuture = response.map( rest => Ok("Got result: " + rest ))
twitterFuture.onSuccess(p.success)
twitterFuture.onFailure(p.failure)
p.future
}
We can generalize this to implicitly convert com.twitter.util.Future[A]
to scala.concurrent.Future[A]
.
import com.twitter.util.{Future => TwitterFuture}
import scala.concurrent.{Future, Promise}
object TwitterConversions {
def twitter2Scala[A](f: TwitterFuture[A]): Future[A] = {
val p = Promise[A]
f.onSuccess(p.success)
f.onFailure(p.failure)
p.future
}
}
Then all you would need to do in your controller is import TwitterConversions._
(or whatever package you put it in).