Convert scala future to java future

后端 未结 3 2277
迷失自我
迷失自我 2021-02-19 09:18

I have a generated java interface containing a method:

public Future getCustomersAsync(AsyncHandler asyncHandler);

I w

3条回答
  •  故里飘歌
    2021-02-19 09:48

    Starting Scala 2.13, the standard library includes scala.jdk.FutureConverters which provides Scala to Java Future conversions (and vice versa):

    import scala.jdk.FutureConverters._
    
    // val scalaFuture: scala.concurrent.Future[Int] = Future.successful(42)
    scalaFuture.asJava
    // java.util.concurrent.CompletionStage[Int] = 
    

    Note for Java users, you'd rather use FutureConverters the explicit way:

    import scala.jdk.javaapi.FutureConverters;
    
    // val scalaFuture: scala.concurrent.Future[Int] = Future.successful(42)
    FutureConverters.asJava(scalaFuture);
    // java.util.concurrent.CompletionStage[Int] = 
    

提交回复
热议问题