Ah great thanks, that's exactly what I wanted to h...
# coroutines
m
Ah great thanks, that's exactly what I wanted to hear 🙂
After 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:
Copy code
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.