Package-level declarations

General-purpose coroutine builders, contexts, and helper functions.

General-purpose coroutine builders, contexts, and helper functions.

General-purpose coroutine builders, contexts, and helper functions.

General-purpose coroutine builders, contexts, and helper functions.

General-purpose coroutine builders, contexts, and helper functions.

General-purpose coroutine builders, contexts, and helper functions.

General-purpose coroutine builders, contexts, and helper functions.

General-purpose coroutine builders, contexts, and helper functions.

Types

Link copied to clipboard

Cancellable continuation is a thread-safe continuation primitive with the support of an asynchronous cancellation.

Link copied to clipboard
expect open class CancellationException(message: String?) : IllegalStateException

Thrown by cancellable suspending functions if the Job of the coroutine is cancelled while it is suspending. It indicates normal cancellation of a coroutine. It is not printed to console/log by default uncaught exception handler. (see CoroutineExceptionHandler).

Thrown by cancellable suspending functions if the Job of the coroutine is cancelled while it is suspending. It indicates normal cancellation of a coroutine. It is not printed to console/log by default uncaught exception handler. See CoroutineExceptionHandler

Thrown by cancellable suspending functions if the Job of the coroutine is cancelled while it is suspending. It indicates normal cancellation of a coroutine. It is not printed to console/log by default uncaught exception handler. (see CoroutineExceptionHandler).

Link copied to clipboard

CoroutineDispatcher that provides a method to close it, causing the rejection of any new tasks and cleanup of all underlying resources associated with the current dispatcher. Examples of closeable dispatchers are dispatchers backed by java.lang.Executor and by kotlin.native.Worker.

CoroutineDispatcher that provides a method to close it, causing the rejection of any new tasks and cleanup of all underlying resources associated with the current dispatcher. Examples of closeable dispatchers are dispatchers backed by java.lang.Executor and by kotlin.native.Worker.

CoroutineDispatcher that provides a method to close it, causing the rejection of any new tasks and cleanup of all underlying resources associated with the current dispatcher. Examples of closeable dispatchers are dispatchers backed by java.lang.Executor and by kotlin.native.Worker.

CoroutineDispatcher that provides a method to close it, causing the rejection of any new tasks and cleanup of all underlying resources associated with the current dispatcher. Examples of closeable dispatchers are dispatchers backed by java.lang.Executor and by kotlin.native.Worker.

Link copied to clipboard

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

Link copied to clipboard
interface CompletableJob : Job

A job that can be completed using complete() function. It is returned by Job() and SupervisorJob() constructor functions.

Link copied to clipboard

A ThreadContextElement copied whenever a child coroutine inherits a context containing it.

Link copied to clipboard

Throwable which can be cloned during stacktrace recovery in a class-specific way. For additional information about stacktrace recovery see STACKTRACE_RECOVERY_PROPERTY_NAME

Link copied to clipboard

Base class to be extended by all coroutine dispatcher implementations.

Link copied to clipboard

An optional element in the CoroutineContext to handle coroutine exceptions without a clear propagation path.

Link copied to clipboard

User-specified name of coroutine. This name is used in debugging mode. See newCoroutineContext for the description of coroutine debugging facilities.

Link copied to clipboard
interface CoroutineScope

A scope in which coroutines run.

Link copied to clipboard

Defines start options for coroutines builders.

Link copied to clipboard
interface Deferred<out T> : Job

Deferred value is a non-blocking cancellable future — it is a Job with a result.

Link copied to clipboard
annotation class DelicateCoroutinesApi

Marks declarations in the coroutines that are delicate — they have limited use-case and shall be used with care in general code. Any use of a delicate declaration has to be carefully reviewed to make sure it is properly used and does not create problems like memory and resource leaks. Carefully read documentation of any declaration marked as DelicateCoroutinesApi.

Link copied to clipboard
expect object Dispatchers

Groups various implementations of CoroutineDispatcher.

actual object Dispatchers

Groups various implementations of CoroutineDispatcher.

actual object Dispatchers

Groups various implementations of CoroutineDispatcher.

actual object Dispatchers

Groups various implementations of CoroutineDispatcher.

Link copied to clipboard
fun interface DisposableHandle

A handle to an allocated object that can be disposed to make it eligible for garbage collection.

Link copied to clipboard

CoroutineDispatcher that has underlying Executor for dispatching tasks. Instances of ExecutorCoroutineDispatcher should be closed by the owner of the dispatcher.

Link copied to clipboard

Marks declarations that are still experimental in coroutines API, which means that the design of the corresponding declarations has open issues which may (or may not) lead to their changes in the future. Roughly speaking, there is a chance that those declarations will be deprecated in the near future or the semantics of their behavior may change in some way that may break some code.

Link copied to clipboard

Marks declarations that cannot be safely inherited from.

Link copied to clipboard

Marks Flow-related API as a feature preview.

Link copied to clipboard
Link copied to clipboard

Marks declarations that are internal in coroutines API, which means that should not be used outside of kotlinx.coroutines, because their signatures and semantics will change between future releases without any warnings and without providing any migration aids.

Link copied to clipboard

Marks declarations that cannot be safely inherited from.

Link copied to clipboard

A background job. Conceptually, a job is a cancellable thing with a lifecycle that concludes in its completion.

Link copied to clipboard

Base class for special CoroutineDispatcher which is confined to application "Main" or "UI" thread and used for any UI-based activities. Instance of MainDispatcher can be obtained by Dispatchers.Main.

Link copied to clipboard

A non-cancelable job that is always active. It is designed for withContext function to prevent cancellation of code blocks that need to be executed without cancellation.

Link copied to clipboard
annotation class ObsoleteCoroutinesApi

Marks declarations that are obsolete in coroutines API, which means that the design of the corresponding declarations has serious known flaws and they will be redesigned in the future. Roughly speaking, these declarations will be deprecated in the future but there is no replacement for them yet, so they cannot be deprecated right away.

Link copied to clipboard
expect fun interface Runnable

A runnable task for CoroutineDispatcher.dispatch.

actual fun interface Runnable

A runnable task for CoroutineDispatcher.dispatch.

actual typealias Runnable = java.lang.Runnable

A runnable task for CoroutineDispatcher.dispatch.

actual fun interface Runnable

A runnable task for CoroutineDispatcher.dispatch.

Link copied to clipboard

Defines elements in a CoroutineContext that are installed into the thread context every time the coroutine with this element in the context is resumed on a thread.

Link copied to clipboard

Properties

Link copied to clipboard

Name of the property that controls coroutine debugging.

Link copied to clipboard

Automatic debug configuration value for DEBUG_PROPERTY_NAME.

Link copied to clipboard

Debug turned off value for DEBUG_PROPERTY_NAME.

Link copied to clipboard

Debug turned on value for DEBUG_PROPERTY_NAME.

Link copied to clipboard

The CoroutineDispatcher that is designed for offloading blocking IO tasks to a shared pool of threads. Additional threads in this pool are created on demand. Default IO pool size is 64; on JVM it can be configured using JVM-specific mechanisms, please refer to Dispatchers.IO documentation on JVM platform.

The CoroutineDispatcher that is designed for offloading blocking IO tasks to a shared pool of threads. Additional threads in this pool are created on demand. Default IO pool size is 64; on JVM it can be configured using JVM-specific mechanisms, please refer to Dispatchers.IO documentation on JVM platform.

Link copied to clipboard

Name of the property that defines the maximal number of threads that are used by Dispatchers.IO coroutines dispatcher.

Link copied to clipboard

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

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

Link copied to clipboard

Retrieves the current Job instance from the given CoroutineContext or throws IllegalStateException if no job is present in the context.

Functions

Link copied to clipboard

Wraps ThreadLocal into ThreadContextElement. The resulting ThreadContextElement maintains the given value of the given ThreadLocal for a coroutine regardless of the actual thread it is resumed on. By default ThreadLocal.get is used as a value for the thread-local variable, but it can be overridden with the value parameter. Beware that the context element does not track modifications of the thread-local and accessing thread-local from a coroutine without the corresponding context element returns an undefined value. See the examples for a detailed description.

Link copied to clipboard

Converts an instance of Window to an implementation of CoroutineDispatcher.

Converts an instance of Executor to an implementation of CoroutineDispatcher.

Converts an instance of ExecutorService to an implementation of ExecutorCoroutineDispatcher.

Link copied to clipboard

Creates a view of this CompletableDeferred as a Deferred, which prevents downcasting to a completable version.

Converts this promise value to the instance of Deferred.

Link copied to clipboard

Converts an instance of CoroutineDispatcher to an implementation of Executor.

Link copied to clipboard

Converts this deferred value to the instance of Promise.

Link copied to clipboard
fun <T> CoroutineScope.async(context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> T): Deferred<T>

Launches a new child coroutine of CoroutineScope without blocking the current thread and returns a reference to the coroutine as a Deferred that can be used to access the result of the coroutine.

fun <T> CoroutineScope.async(context: Job, start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> T): Deferred<T>

Deprecated version of async that accepts a Job.

fun <T> CoroutineScope.async(context: NonCancellable, start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> T): Deferred<T>

Deprecated version of async that accepts a NonCancellable.

Link copied to clipboard
suspend fun <T : JsAny?> Promise<T>.await(): T

Awaits for completion of the promise without blocking.

Link copied to clipboard
suspend fun <T> awaitAll(vararg deferreds: Deferred<T>): List<T>

Awaits for completion of given deferred values without blocking a thread and resumes normally with the list of values when all deferred computations are complete or resumes with the first thrown exception if any of computations complete exceptionally including cancellation.

Link copied to clipboard
suspend fun <T> Collection<Deferred<T>>.awaitAll(): List<T>

Awaits for completion of given deferred values without blocking a thread and resumes normally with the list of values when all deferred computations are complete or resumes with the first thrown exception if any of computations complete exceptionally including cancellation.

Link copied to clipboard

Suspends coroutine until next JS animation frame and returns frame time on resumption. The time is consistent with window.performance.now(). This function is cancellable. If the Job of the current coroutine is completed while this suspending function is waiting, this function immediately resumes with CancellationException.

Link copied to clipboard
suspend fun awaitCancellation(): Nothing

Suspends until cancellation, in which case it will throw a CancellationException.

Link copied to clipboard

Cancels Job of this context with an optional cancellation cause. See Job.cancel for details.

Cancels this scope, including its job and all its children with an optional cancellation cause.

fun CoroutineScope.cancel(message: String, cause: Throwable? = null)

Cancels this scope with a CancellationException with the given message and cause.

fun Job.cancel(message: String, cause: Throwable? = null)

Cancels current job, including all its children with a specified diagnostic error message. A cause can be specified to provide additional details on a cancellation reason for debugging purposes.

Link copied to clipboard
suspend fun Job.cancelAndJoin()

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

Link copied to clipboard

Cancels all children of the Job in this context, without touching the state of this job itself with an optional cancellation cause. See Job.cancel. It does not do anything if there is no job in the context or it has no children.

Cancels all children jobs of this coroutine using Job.cancel for all of them with an optional cancellation cause. Unlike Job.cancel on this job as a whole, the state of this job itself is not affected.

Link copied to clipboard

Cancels a specified future when this job is cancelled. This is a shortcut for the following code with slightly more efficient implementation (one fewer object created).

Link copied to clipboard

Creates a cancellation exception with a specified message and cause.

Link copied to clipboard

Creates an already completed CompletableDeferred with a given value.

fun <T> CompletableDeferred(parent: Job? = null): CompletableDeferred<T>

Creates a CompletableDeferred in an active state. It is optionally a child of a parent job.

Link copied to clipboard

Completes this deferred value with the value or exception in the given result. Returns true if this deferred was completed as a result of this invocation and false otherwise (if it was already completed).

Link copied to clipboard
Link copied to clipboard

Creates a CoroutineScope with the given coroutine context.

Link copied to clipboard
suspend fun <R> coroutineScope(block: suspend CoroutineScope.() -> R): R

Runs the given block in-place in a new CoroutineScope based on the caller coroutine context, returning its result.

Link copied to clipboard

Returns the current CoroutineContext retrieved by using kotlin.coroutines.coroutineContext.

Link copied to clipboard
suspend fun delay(timeMillis: Long)

Delays coroutine for at least the given time without blocking a thread and resumes it after a specified time. If the given timeMillis is non-positive, this function returns immediately.

suspend fun delay(duration: Duration)

Delays coroutine for at least the given duration without blocking a thread and resumes it after the specified time. If the given duration is non-positive, this function returns immediately.

Link copied to clipboard

Ensures that job in the current context is active.

Throws the CancellationException that was the scope's cancellation cause if the scope is no longer active.

Ensures that current job is active. If the job is no longer active, throws CancellationException. If the job was cancelled, thrown exception contains the original cancellation cause.

Link copied to clipboard
inline suspend fun ThreadLocal<*>.ensurePresent()

Checks whether current thread local is present in the coroutine context and throws IllegalStateException if it is not. It is a good practice to validate that thread local is present in the context, especially in large code-bases, to avoid stale thread-local values and to have a strict invariants.

Link copied to clipboard
inline suspend operator fun <T> CoroutineDispatcher.invoke(noinline block: suspend CoroutineScope.() -> T): T

Calls the specified suspending block with the given CoroutineDispatcher, suspends until it completes, and returns the result.

Link copied to clipboard
inline suspend fun ThreadLocal<*>.isPresent(): Boolean

Return true when current thread local is present in the coroutine context, false otherwise. Thread local can be present in the context only if it was added via asContextElement to the context.

Link copied to clipboard
fun Job(parent: Job? = null): CompletableJob

Creates a job object in an active state. A failure of any child of this job immediately causes this job to fail, too, and cancels the rest of its children.

Link copied to clipboard
suspend fun joinAll(vararg jobs: Job)

Suspends current coroutine until all given jobs are complete. This method is semantically equivalent to joining all given jobs one by one with jobs.forEach { it.join() }.

Link copied to clipboard
suspend fun Collection<Job>.joinAll()

Suspends current coroutine until all given jobs are complete. This method is semantically equivalent to joining all given jobs one by one with forEach { it.join() }.

Link copied to clipboard
fun CoroutineScope.launch(context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> Unit): Job

Launches a new child coroutine of CoroutineScope without blocking the current thread and returns a reference to the coroutine as a Job.

fun CoroutineScope.launch(context: Job, start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> Unit): Job

Deprecated version of launch that accepts a Job.

fun CoroutineScope.launch(context: NonCancellable, start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> Unit): Job

Deprecated version of launch that accepts a NonCancellable.

Link copied to clipboard

Creates a CoroutineScope for scheduling UI updates.

Link copied to clipboard

Creates a context for a new coroutine.

Creates a context for a new coroutine.

Creates a context for a new coroutine.

Creates a context for a new coroutine.

Link copied to clipboard

Creates a coroutine execution context with the fixed-size thread-pool and built-in yield support. NOTE: The resulting CoroutineDispatcher owns native resources (its threads). Resources are reclaimed by CloseableCoroutineDispatcher.close.

Creates a coroutine execution context with the fixed-size thread-pool and built-in yield support. NOTE: The resulting CoroutineDispatcher owns native resources (its threads). Resources are reclaimed by CloseableCoroutineDispatcher.close.

Creates a coroutine execution context with the fixed-size thread-pool and built-in yield support. NOTE: The resulting CoroutineDispatcher owns native resources (its threads). Resources are reclaimed by CloseableCoroutineDispatcher.close.

Link copied to clipboard

Creates a coroutine execution context using a single thread with built-in yield support. NOTE: The resulting CloseableCoroutineDispatcher owns native resources (its thread). Resources are reclaimed by CloseableCoroutineDispatcher.close.

Link copied to clipboard

Creates a new view of this CoroutineScope, but with the specified context added to it.

Link copied to clipboard
fun <T> CoroutineScope.promise(context: Job, start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> T): Promise<T>

Deprecated version of promise that accepts a Job.

fun <T : JsAny?> CoroutineScope.promise(context: Job, start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> T): Promise<T>

Deprecated version of promise that accepts a Job.

fun <T : JsAny?> CoroutineScope.promise(context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> T): Promise<T>

Starts new coroutine and returns its result as an implementation of Promise.

Link copied to clipboard
@JvmName(name = "runBlockingK")
fun <T> runBlocking(context: CoroutineContext = EmptyCoroutineContext, block: suspend CoroutineScope.() -> T): T

Runs the given block in-place in a new coroutine based on context, blocking the current thread until its completion, and then returning its result.

Link copied to clipboard
suspend fun <T> runInterruptible(context: CoroutineContext = EmptyCoroutineContext, block: () -> T): T

Calls the specified block with a given coroutine context in an interruptible manner. The blocking code block will be interrupted and this function will throw CancellationException if the coroutine is cancelled.

suspend fun <T> runInterruptible(context: Job, block: () -> T): T

Deprecated version of runInterruptible that accepts a Job.

Link copied to clipboard
fun SupervisorJob(parent: Job? = null): CompletableJob

Creates a supervisor job object in an active state. Children of a supervisor job can fail independently of each other.

Link copied to clipboard
suspend fun <R> supervisorScope(block: suspend CoroutineScope.() -> R): R

Runs the given block in-place in a new CoroutineScope that contains a SupervisorJob and is based on the caller coroutine context. The result of block is returned.

Link copied to clipboard
inline suspend fun <T> suspendCancellableCoroutine(crossinline block: (CancellableContinuation<T>) -> Unit): T

Suspends the coroutine like suspendCoroutine, but providing a CancellableContinuation to the block. This function throws a CancellationException if the Job of the coroutine is cancelled or completed while it is suspended, or if CancellableContinuation.cancel is invoked.

Link copied to clipboard
suspend fun <T> withContext(context: CoroutineContext, block: suspend CoroutineScope.() -> T): T

Calls the specified suspending block with an updated coroutine context, suspends until it completes, and returns the result.

Link copied to clipboard
suspend fun <T> withTimeout(timeMillis: Long, block: suspend CoroutineScope.() -> T): T

Shorthand form for calling withTimeout with a Duration timeout of timeMillis milliseconds. Please see the overload accepting a Duration for details.

suspend fun <T> withTimeout(timeout: Duration, block: suspend CoroutineScope.() -> T): T

Calls the given suspending block with the specified timeout, suspends until it completes, and returns the result.

Link copied to clipboard
suspend fun <T> withTimeoutOrNull(timeMillis: Long, block: suspend CoroutineScope.() -> T): T?

Shorthand form for calling withTimeoutOrNull with a Duration timeout of timeMillis milliseconds. Please see the overload accepting a Duration for details.

suspend fun <T> withTimeoutOrNull(timeout: Duration, block: suspend CoroutineScope.() -> T): T?

Calls the given suspending block with the specified timeout, suspends until it completes, and returns the result.

Link copied to clipboard
suspend fun yield()

Suspends this coroutine and immediately schedules it for further execution.