I have a Java Future
object which I would like to convert into a Scala Future
.
Looking at the j.u.c.Future
API, there is nothing much that I could use other than the isDone
method. Is this isDone
method blocking?
Currently this is what I have in my mind:
val p = Promise()
if (javaFuture.isDone())
p.success(javaFuture.get)
Is there a better way to do this?
Starting Scala 2.13
, the standard library includes scala.jdk.FutureConverters
which provides Java to Scala CompletableFuture/Future
implicit conversions:
import scala.jdk.FutureConverters._
// val javaFuture = java.util.concurrent.CompletableFuture.completedFuture(12)
val scalaFuture = javaFuture.asScala
// scalaFuture: scala.concurrent.Future[Int] = Future(Success(12))