voddan
import org.springframework.util.concurrent.ListenableFuture import org.springframework.util.concurrent.ListenableFutureCallback public suspend fun <T> ListenableFuture<T>.await(): T = suspendCancellableCoroutine { cont: CancellableContinuation<T> -> val callback = ContinuationCallback(cont) this.addCallback(callback) cont.invokeOnCompletion { callback.cont = null // clear the reference to continuation from the future's callback } } private class ContinuationCallback<T>(@Volatile @JvmField var cont: CancellableContinuation<T>?) : ListenableFutureCallback<T> { @Suppress("UNCHECKED_CAST") override fun onSuccess(result: T?) { cont?.resume(result as T) } override fun onFailure(t: Throwable) { cont?.resumeWithException(t) } }
Vsevolod Tolstopyatov [JB]
if (isDone) return get()
suspendCancellableCoroutine
suspend
await
A modern programming language that makes developers happier.