CompletableDeferred

interface CompletableDeferred<T> : Deferred<T> (source)

A Deferred that can be completed via public functions complete or cancel.

Note, that complete functions returns false when this deferred value is already complete or completing, while cancel returns true as long the deferred is still cancelling and the corresponding exception is incorporated into the final completion exception.

An instance of completable deferred can be created by CompletableDeferred() function in active state.

All functions on this interface and on all interfaces derived from it are thread-safe and can be safely invoked from concurrent coroutines without external synchronization.

Inherited Properties

onAwait

abstract val onAwait: SelectClause1<T>

Clause for select expression of await suspending function that selects with the deferred value when it is resolved. The select invocation fails if the deferred value completes exceptionally (either fails or it cancelled).

Functions

complete

abstract fun complete(value: T): Boolean

Completes this deferred value with a given value. The result is true if this deferred was completed as a result of this invocation and false otherwise (if it was already completed).

completeExceptionally

abstract fun completeExceptionally(
    exception: Throwable
): Boolean

Completes this deferred value exceptionally with a given exception. The result is true if this deferred was completed as a result of this invocation and false otherwise (if it was already completed).

Inherited Functions

await

abstract suspend fun await(): T

Awaits for completion of this value without blocking a thread and resumes when deferred computation is complete, returning the resulting value or throwing the corresponding exception if the deferred was cancelled.

getCompleted

abstract fun getCompleted(): T

Returns completed result or throws IllegalStateException if this deferred value has not completed yet. It throws the corresponding exception if this deferred was cancelled.

getCompletionExceptionOrNull

abstract fun getCompletionExceptionOrNull(): Throwable?

Returns completion exception result if this deferred was cancelled and has completed, null if it had completed normally, or throws IllegalStateException if this deferred value has not completed yet.

Extension Properties

isActive

Returns true when the Job of the coroutine in this context is still active (has not completed and was not cancelled yet).

Extension Functions

cancel

fun CoroutineContext.cancel(): Unit

Cancels Job of this context. See Job.cancel for details.

cancelAndJoin

suspend fun Job.cancelAndJoin(): Unit

Cancels the job and suspends invoking coroutine until the cancelled job is complete.