Search code examples
kotlinkotlin-coroutinesvert.xvert.x-webclient

How to convert a CompletableFuture to a Vert.X Future


I'm trying to execute a db transaction with the vertx reactive sql client in a coroutine. Somehow I can't figure out how I can convert the CompletableFuture to the desired io.vertx.core.Future type. Are there any helper methods or extensions to do this easily ?

val client : PgPool
... 

suspend fun someServiceFunction () {
    coroutineScope {
        client.withTransaction { connection ->
            val completableFuture = async {
                repository.save(connection, requestDTO)  //This is a suspend function
            }.asCompletableFuture()

            //Return type has to be a io.vertx.core.Future
            //How can I transform the completableFuture to it ?
        }
    }
}

Thank you for your help !


Solution

  • Vert.x Future has a conversion method:

    future = Future.fromCompletionStage(completionStage, vertxContext)