[CompletableFuture] I’m having issues converting C...
# getting-started
m
[CompletableFuture] I’m having issues converting CompletableFuture to a suspendable function as there aren’t any examples nor guides for it. Does someone know what is the proper way to handle this? I’m using the Java 11 Http client and creating a wrapper for calling an API.
Copy code
suspend fun HackerNewsApi.fetchItem(id: Int): CompletableFuture<HttpResponse<String>> {
    return client.sendAsync(
        request("${this.domain}/v0/item/$id.json"),
        HttpResponse.BodyHandlers.ofString()
    )
}
n
Copy code
suspend fun HackerNewsApi.fetchItem(id: Int): HttpResponse<String> {
    return client.sendAsync(
        request("${this.domain}/v0/item/$id.json"),
        HttpResponse.BodyHandlers.ofString()
    ).asDeferred().await()
}
d
no need to convert to deferred, you can just use await extension function directly on the future (from https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-jdk8/)
👍 2