james
01/19/2021, 2:28 PMFuture
or CompletableFuture
in JVM > 8 within suspendCoroutine
what's the suggested pattern for interaction. From what I can tell, the suspendCoroutine
is designed for an asynchronous callback but from what I can tell JDK 11, 12, 13 and 14 don't have a way to implement a CompletionStage interface.fun <T> Future<T>.await(): T = this.get()
But this seems inherently incorrect and i'm looking for some suggestions on ways to improve this.louiscad
01/19/2021, 3:06 PMjames
01/19/2021, 4:18 PMFuture
louiscad
01/19/2021, 5:20 PMget
in <http://Dispatchers.IO|Dispatchers.IO> { }
.james
01/19/2021, 6:03 PMsuspend fun <T> Future<T>.await(): T = coroutineScope {
// If coroutine is cancelled, cancel the future as well
val cancelJob = launch {
suspendCancellableCoroutine { cont ->
cont.invokeOnCancellation {
this@await.cancel(true)
}
}
}
while (!isDone) {
delay(1)
}
cancelJob.cancel()
this@await.get()
}
But obviously that's still sub-optimal.louiscad
01/19/2021, 6:05 PMYigitunlu
01/20/2021, 7:46 AMmyungpyo.shim
01/21/2021, 5:36 AM