Is there any way to convert guava ListenableFuture...
# coroutines
m
Is there any way to convert guava ListenableFuture into Kotlin Deferred? I see that there is an extension method await on ListenableFuture in kotlin-coroutines-guava but it is not what I need
d
Copy code
val deferred = async { future.await() }
👍 2
v
also, you can use LF’s callback:
Copy code
fun <T> ListenableFuture<T>.toDeferred(): Deferred<T?>  {
        val deferred = CompletableDeferred<T?>()
        Futures.addCallback(this, object : FutureCallback<T> {
            override fun onSuccess(result: T?) {
                deferred.complete(result)
            }

            override fun onFailure(t: Throwable?) {
                deferred.completeExceptionally(t!!)
            }
        } )
        return deferred
    }
to avoid unnecessary launch (~context switch)
m
thank you