CancellableContinuation
interface CancellableContinuation<in T> :
Continuation<T>,
Job
(source)Cancellable continuation. Its job is completed when it is resumed or cancelled. When cancel function is explicitly invoked, this continuation immediately resumes with CancellationException or with the specified cancel cause.
Cancellable continuation has three states (as subset of Job states):
State | isActive | isCompleted | isCancelled |
---|---|---|---|
Active (initial state) | true |
false |
false |
Resumed (final completed state) | false |
true |
false |
Canceled (final completed state) | false |
true |
true |
Invocation of cancel transitions this continuation from active to cancelled state, while invocation of resume or resumeWithException transitions it from active to resumed state.
A cancelled continuation implies that it is completed.
Invocation of resume or resumeWithException in resumed state produces IllegalStateException but is ignored in cancelled state.
+-----------+ resume +---------+ | Active | ----------> | Resumed | +-----------+ +---------+ | | cancel V +-----------+ | Cancelled | +-----------+
Properties
abstract val isActive: Boolean Returns |
|
abstract val isCancelled: Boolean Returns |
|
abstract val isCompleted: Boolean Returns |
Inherited Properties
Returns a sequence of this job’s children. |
|
abstract val onJoin: SelectClause0 Clause for select expression of join suspending function that selects when the job is complete. This clause never fails, even if the job completes exceptionally. |
Functions
Cancels this continuation with an optional cancellation cause. The result is |
|
abstract fun initCancellability(): Unit Makes this continuation cancellable. Use it with |
|
abstract fun invokeOnCompletion( Registers handler that is synchronously invoked once on completion of this continuation.
When continuation is already complete, then the handler is immediately invoked
with continuation’s exception or |
|
abstract fun CoroutineDispatcher.resumeUndispatched( Resumes this continuation with a given value in the invoker thread without going though dispatch function of the CoroutineDispatcher in the context. This function is designed to be used only by the CoroutineDispatcher implementations themselves. It should not be used in general code. |
|
abstract fun CoroutineDispatcher.resumeUndispatchedWithException( Resumes this continuation with a given exception in the invoker thread without going though dispatch function of the CoroutineDispatcher in the context. This function is designed to be used only by the CoroutineDispatcher implementations themselves. It should not be used in general code. |
|
Inherited Functions
abstract fun |
|
abstract fun getCancellationException(): CancellationException Returns CancellationException that signals the completion of this job. This function is used by cancellable suspending functions. They throw exception returned by this function when they suspend in the context of this job and this job becomes complete. |
|
abstract fun invokeOnCompletion( Registers handler that is synchronously invoked once on cancellation or completion of this job.
When job is already cancelling or complete, then the handler is immediately invoked
with a job’s cancellation cause or |
|
abstract suspend fun join(): Unit Suspends coroutine until this job is complete. This invocation resumes normally (without exception) when the job is complete for any reason and the Job of the invoking coroutine is still active. This function also starts the corresponding coroutine if the Job was still in new state. |
|
abstract fun start(): Boolean Starts coroutine related to this job (if any) if it was not started yet.
The result |
Extension Functions
fun CoroutineContext.cancel( Cancels Job of this context with an optional cancellation cause. The result is |
|
Cancels the job and suspends invoking coroutine until the cancelled job is complete. |
|
fun Job.cancelFutureOnCompletion( Cancels a specified future when this job is complete. |
|
fun Job.disposeOnCompletion( Disposes a specified handle when this job is complete. |
|
Suspends coroutine until all children of this job are complete using Job.join for all of them. Unlike Job.join on this job as a whole, it does not wait until this job is complete. |