ListenableFuture to scala Future

后端 未结 2 383
暗喜
暗喜 2021-02-02 08:34

I am in the process of writing a small scala wrapper around a java library.

The java library has an object QueryExecutor exposing 2 methods:

  • execute(query)
2条回答
  •  闹比i
    闹比i (楼主)
    2021-02-02 09:09

    Another, slightly shorter solution:

    implicit class ListenableFutureDecorator[T](val f: ListenableFuture[T]) extends AnyVal {
      def asScala(implicit e: Executor): Future[T] = {
        val p = Promise[T]()
        f.addListener(() => p.complete(Try(f.get())), e)
        p.future
      }
    }
    

提交回复
热议问题