resume

abstract fun resume(value: T, onCancellation: (cause: Throwable) -> Unit?)

Resumes this continuation with the specified value and calls the specified onCancellation handler when either resumed too late (when continuation was already cancelled) or, although resumed successfully (before cancellation), the coroutine's job was cancelled before it had a chance to run in its dispatcher, so that the suspended function threw an exception instead of returning this value.

The installed onCancellation handler should not throw any exceptions. If it does, they will get caught, wrapped into a CompletionHandlerException and processed as an uncaught exception in the context of the current coroutine (see CoroutineExceptionHandler).

This function shall be used when resuming with a resource that must be closed by code that called the corresponding suspending function, for example:

continuation.resume(resource) {
resource.close()
}

A more complete example and further details are given in the documentation for the suspendCancellableCoroutine function.

Note: The onCancellation handler must be fast, non-blocking, and thread-safe. It can be invoked concurrently with the surrounding code. There is no guarantee on the execution context of its invocation.

Sources

common source
Link copied to clipboard