madsbf
11/02/2016, 9:54 AMAfter await() returns, the coroutine is suspended and the control is transferred to its caller. The execution of the coroutine is resumed only when the future f is completed, and resume() is called on the continuation c (as per the handler registered with the CompletableFuture object).So I should be able to do something like below to return the value without suspending at all, if the CompletableFuture is already completed, since resume() is called before await() returns:
suspend fun <T> await(f: CompletableFuture<T>, c: Continuation<T>) {
if (f.isCompleted()) {
c.resume(f.value)
}
f.whenComplete { result, throwable ->
if (throwable == null)
// the future has been completed normally
c.resume(result)
else
// the future has completed with an exception
c.resumeWithException(throwable)
}
}
Missing error handling, but that's just the basic idea behind it.