voddan
04/23/2018, 2:36 PMimport 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]
04/23/2018, 3:26 PMif (isDone) return get()
before suspendCancellableCoroutine
voddan
04/24/2018, 7:39 AMsuspend
methods executed in the reverse order of how they are written. Glad to know it is not about my await
🙂