https://kotlinlang.org logo
#coroutines
Title
# coroutines
a

andrea.santurbano

03/27/2019, 8:19 AM
Hi everyone I'm working with Kafka via Kotlin and we want to transform its
Future
implementation (https://github.com/apache/kafka/blob/trunk/clients/src/main/java/org/apache/kafka/clients/producer/internals/FutureRecordMetadata.java) as
Deferred
. Could this be a solution?
Copy code
fun <T> Future<T>.asDeferred(): Deferred<T> {
    return GlobalScope.async(<http://Dispatchers.IO|Dispatchers.IO>) {
        get()
    }
}
@rocketraman suggested to follow also this solution: https://stackoverflow.com/questions/50793362/how-to-use-suspendcoroutine-to-turn-java-7-future-into-kotlin-suspending-functio#answer-52901712 Which of these two is the most correct?
h

hmole

03/27/2019, 8:35 AM
Maybe just wrap it in
CompletableFuture
and use coroutine jdk8 wrappers
a

andrea.santurbano

03/27/2019, 9:42 AM
Why add a java layer?
r

rocketraman

03/27/2019, 12:16 PM
Wrapping a regular
Future
in
CompletableFuture
doesn't solve the underlying problem... whenever and wherever that
get
is called, its gonna block. That's why I suggested he use the IO dispatcher for the
get
.
123 Views