Cancellable continuation. It is completed when resumed or cancelled. When the cancel function is explicitly invoked, this continuation immediately resumes with a CancellationException or the specified cancel cause.
An instance of
CancellableContinuation is created by the suspendCancellableCoroutine function.
Cancellable continuation has three states (as subset of Job states):
|Active (initial state)|
|Resumed (final completed state)|
|Canceled (final completed state)|
Invocation of cancel transitions this continuation from active to cancelled state, while invocation of Continuation.resume or Continuation.resumeWithException transitions it from active to resumed state.
+-----------+ resume +---------+
| Active | ----------> | Resumed |
| Cancelled |
Registers a handler to be synchronously invoked on cancellation (regular or exceptional) of this continuation. When the continuation is already cancelled, the handler is immediately invoked with the cancellation exception. Otherwise, the handler will be invoked as soon as this continuation is cancelled.
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.