codebijan
12/27/2019, 10:03 PMXavier Hanin
01/06/2020, 11:15 AM/**
* Converts this api future to the instance of [CompletableFuture].
* Inspired by Kotlin coroutines Deferred<T>.asCompletableFuture().
*/
fun <T> ApiFuture<T>.asCompletableFuture(): CompletableFuture<T> {
val future = CompletableFuture<T>()
addListener({
try {
future.complete(get())
} catch (t: CancellationException) {
future.cancel(true)
} catch (t: InterruptedException) {
future.cancel(true)
} catch (t: ExecutionException) {
future.completeExceptionally(t.cause)
} catch (t: Throwable) {
future.completeExceptionally(t)
}
}, {r:Runnable -> r.run()})
return future
}
suspend fun <T> ApiFuture<T>.await(): T = asCompletableFuture().await()
Gabriel Feo
01/15/2020, 8:52 AM