What is the best way to get a future into the coro...
# coroutines
p
What is the best way to get a future into the coroutine world?
j
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
With Future I mean just Future.
j
What I said is correct for
java.util.concurrent.Future
too
(But only with Java8)
p
I don't get it. CompletionStage is != Future?
There is no java.util.concurrent.Future.await()
j
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() }