A: Yes.
BECAUSE
org.jooq.ResultQuery<R extends Record>
has a @NotNull CompletionStage<Result<R>> fetchAsync()
` method which hooks into Java's (JDK 8+) machinery for 'futures'.
AND
kotlinx-coroutines-jdk8
provides a bunch of extension methods for adapting between Kotlin suspending functions and JDK 8+ futures.
THEREFORE we can do:
import kotlinx.coroutines.future.await
...
suspend fun myQuery() {
return dsl.select()
//.etc()
.fetchAsync()
.await() // <- IS a suspending call !!!
}
It should be noted that there are a bunch of overloaded fetchX()
methods on ResultQuery
which provide lots of utility to synchronous calls, but they are not similarly overloaded for fetchAsync()
. That's where the Kotlin programmer may wish to be familiar with the Java futures machinery: any sort of manipulation can be accomplished asynchronously using the thenApply {}
method on CompletionStage<T>
. For example, mapping the results:
suspend fun myQuery() {
return dsl.select()
//.etc()
.fetchAsync()
.thenApply { it.map(mapping(::Film)) } // <- done as part of the 'suspend'
.await()
}
although it should be fine to do it after the suspend:
suspend fun myQuery() {
val records = dsl.select()
//.etc()
.fetchAsync()
.await()
return records.map(mapping(::Film))
}