codebijan
Xavier Hanin
/** * 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
A modern programming language that makes developers happier.