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

Paul Woitaschek

03/06/2018, 4:20 PM
What is the best way to get a future into the coroutine world?
j

Jonathan

03/06/2018, 4:22 PM
With a java8 Future, you can use kotlinx-coroutines-jdk8 https://github.com/Kotlin/kotlinx.coroutines/blob/master/integration/kotlinx-coroutines-jdk8/README.md and do either
CompletionStage.await
or
CompletionStage.asDeferred()
p

Paul Woitaschek

03/06/2018, 4:22 PM
With Future I mean just Future.
j

Jonathan

03/06/2018, 4:23 PM
What I said is correct for
java.util.concurrent.Future
too
(But only with Java8)
p

Paul Woitaschek

03/06/2018, 4:28 PM
I don't get it. CompletionStage is != Future?
There is no java.util.concurrent.Future.await()
j

Jonathan

03/06/2018, 4:29 PM
java.util.concurrent.Future
implement
CompletionStage
but only since Java8
Yes there is an
await
on
CompletionStage
. But you have to add
kotlinx-coroutines-jdk8
as a dependency of your project
no sorry I'm wrong.CompletionStage implements Future
but Future doesn't implement CompletionStage. So if you use
java.util.concurrent.Future
your only option is to create a task to wait on the result:
async { future.get() }
16 Views