CompletableDeferred

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

A Deferred that can be completed via public functions complete, completeExceptionally, and cancel.

Completion functions return false when this deferred value is already complete or completing.

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

isCompletedExceptionally

abstract val isCompletedExceptionally: Boolean

Returns true if computation of this deferred value has completed exceptionally – it had either failed with exception during computation or was cancelled.

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 had completed exceptionally.

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 has completed exceptionally.

getCompletionExceptionOrNull

abstract fun getCompletionExceptionOrNull(): Throwable?

Returns completion exception result if this deferred completed exceptionally, null if it is completed normally, or throws IllegalStateException if this deferred value has not completed yet.

Extension Functions

asPromise

fun <T> Deferred<T>.asPromise(): Promise<T>

Converts this deferred value to the instance of Promise.

cancel

fun CoroutineContext.cancel(
    cause: Throwable? = null
): Boolean

Cancels Job of this context with an optional cancellation cause. The result is true if the job was cancelled as a result of this invocation and false if there is no job in the context or if it was already cancelled or completed. See Job.cancel for details.

cancelAndJoin

suspend fun Job.cancelAndJoin(): Unit

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

cancelFutureOnCompletion

fun Job.cancelFutureOnCompletion(
    future: Future<*>
): DisposableHandle

Cancels a specified future when this job is complete.

disposeOnCompletion

fun Job.disposeOnCompletion(
    handle: DisposableHandle
): DisposableHandle

Disposes a specified handle when this job is complete.

joinChildren

suspend fun Job.joinChildren(): Unit

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.