I want to change the return type of the method below to Future[Unit]
def send(data: Array[Byte]): CompletableFuture[Void] = { val f:CompletableFuture[Void] = client.send(data) f }
I'm using Scala 2.12.1. Is there any convenient way to convert from Java 8 CompletableFuture
to Scala's Future
?
Yes, it's called scala-java8-compat, and here's the method you're looking for.
Though 2.12 was mentioned by asker (in 2017), now we have 2.13 and the conversion has become easier:
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 scalaFuture = javaFuture.asScala
Convert a Java Future to a Scala Future
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With