Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert CompletableFuture (Java 8) to Future (Scala)

Tags:

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?

like image 909
Sebastian Avatar asked Jan 05 '17 18:01

Sebastian


2 Answers

Yes, it's called scala-java8-compat, and here's the method you're looking for.

like image 53
Viktor Klang Avatar answered Oct 01 '22 11:10

Viktor Klang


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

like image 23
akauppi Avatar answered Oct 01 '22 11:10

akauppi