Anyone tried fetching firestore documents and have...
# server
c
Anyone tried fetching firestore documents and have reference as to how to await for a pending fetch?
x
@codebijan coming a bit late, but I use extension function for that:
Copy code
/**
 * 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()
g
There's also a coroutines integration library for Google's Task API