Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions integration/kotlinx-coroutines-guava/src/ListenableFuture.kt
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,19 @@ public fun <T> ListenableFuture<T>.asDeferred(): Deferred<T> {
val deferred = CompletableDeferred<T>()
Futures.addCallback(this, object : FutureCallback<T> {
override fun onSuccess(result: T) {
runCatching { deferred.complete(result) }
.onFailure { handleCoroutineException(EmptyCoroutineContext, it) }
try {
deferred.complete(result)
} catch (e: Exception) {
handleCoroutineException(EmptyCoroutineContext, e)
}
}

override fun onFailure(t: Throwable) {
runCatching { deferred.completeExceptionally(t) }
.onFailure { handleCoroutineException(EmptyCoroutineContext, it) }
try {
deferred.completeExceptionally(t)
} catch (e: Exception) {
handleCoroutineException(EmptyCoroutineContext, e)
}
}
}, MoreExecutors.directExecutor())

Expand Down Expand Up @@ -268,6 +274,8 @@ private class ToContinuation<T>(
// Future. Anything else showing up here indicates a very fundamental bug in a
// Future implementation.
continuation.resumeWithException(e.nonNullCause())
} catch (e: CancellationException) {
continuation.cancel(e)
}
}
}
Expand Down