W3cubDocs

/Kotlin

Package kotlin.coroutines

Basic primitives for creating and suspending coroutines: Continuation, CoroutineContext interfaces, coroutine creation and suspension top-level functions.

Types

Platform and version requirements: JVM (1.3), JS (1.3), Native (1.3)

AbstractCoroutineContextElement

Base class for CoroutineContext.Element implementations.

abstract class AbstractCoroutineContextElement : Element
Platform and version requirements: JVM (1.3), JS (1.3), Native (1.3)

AbstractCoroutineContextKey

Base class for CoroutineContext.Key associated with polymorphic CoroutineContext.Element implementation. Polymorphic element implementation implies delegating its get and minusKey to getPolymorphicElement and minusPolymorphicKey respectively.

abstract class AbstractCoroutineContextKey<B : Element, E : B> : 
    Key<E>
Platform and version requirements: JVM (1.3), JS (1.3), Native (1.3)

Continuation

Interface representing a continuation after a suspension point that returns a value of type T.

interface Continuation<in T>
Platform and version requirements: JVM (1.3), JS (1.3), Native (1.3)

ContinuationInterceptor

Marks coroutine context element that intercepts coroutine continuations. The coroutines framework uses ContinuationInterceptor.Key to retrieve the interceptor and intercepts all coroutine continuations with interceptContinuation invocations.

interface ContinuationInterceptor : Element
Platform and version requirements: JVM (1.3), JS (1.3), Native (1.3)

CoroutineContext

Persistent context for the coroutine. It is an indexed set of Element instances. An indexed set is a mix between a set and a map. Every element in this set has a unique Key.

interface CoroutineContext
Platform and version requirements: JVM (1.3), JS (1.3), Native (1.3)

EmptyCoroutineContext

An empty coroutine context.

object EmptyCoroutineContext : CoroutineContext, Serializable
Platform and version requirements: Native (1.3)

SuspendFunction

Represents a value of a functional type, such as a lambda, an anonymous function or a function reference.

interface SuspendFunction<out R>

Annotations

Platform and version requirements: JVM (1.3), JS (1.3), Native (1.3)

RestrictsSuspension

Classes and interfaces marked with this annotation are restricted when used as receivers for extension suspend functions. These suspend extensions can only invoke other member or extension suspend functions on this particular receiver and are restricted from calling arbitrary suspension functions.

annotation class RestrictsSuspension

Properties

Platform and version requirements: JVM (1.3), JS (1.3), Native (1.3)

coroutineContext

Returns the context of the current coroutine.

suspend val coroutineContext: CoroutineContext

Functions

Platform and version requirements: JVM (1.3), JS (1.3), Native (1.3)

Continuation

Creates a Continuation instance with the given context and implementation of resumeWith method.

fun <T> Continuation(
    context: CoroutineContext, 
    resumeWith: (Result<T>) -> Unit
): Continuation<T>
Platform and version requirements: JVM (1.3), JS (1.3), Native (1.3)

createCoroutine

Creates a coroutine without a receiver and with result type T. This function creates a new, fresh instance of suspendable computation every time it is invoked.

fun <T> (suspend () -> T).createCoroutine(
    completion: Continuation<T>
): Continuation<Unit>

Creates a coroutine with receiver type R and result type T. This function creates a new, fresh instance of suspendable computation every time it is invoked.

fun <R, T> (suspend R.() -> T).createCoroutine(
    receiver: R, 
    completion: Continuation<T>
): Continuation<Unit>
Platform and version requirements: JVM (1.3), JS (1.3), Native (1.3)

getPolymorphicElement

Returns the current element if it is associated with the given key in a polymorphic manner or null otherwise. This method returns non-null value if either Element.key is equal to the given key or if the key is associated with Element.key via AbstractCoroutineContextKey. See AbstractCoroutineContextKey for the example of usage.

fun <E : Element> Element.getPolymorphicElement(
    key: Key<E>
): E?
Platform and version requirements: JVM (1.3), JS (1.3), Native (1.3)

minusPolymorphicKey

Returns empty coroutine context if the element is associated with the given key in a polymorphic manner or null otherwise. This method returns empty context if either Element.key is equal to the given key or if the key is associated with Element.key via AbstractCoroutineContextKey. See AbstractCoroutineContextKey for the example of usage.

fun Element.minusPolymorphicKey(
    key: Key<*>
): CoroutineContext
Platform and version requirements: JVM (1.3), JS (1.3), Native (1.3)

resume

Resumes the execution of the corresponding coroutine passing value as the return value of the last suspension point.

fun <T> Continuation<T>.resume(value: T)
Platform and version requirements: JVM (1.3), JS (1.3), Native (1.3)

resumeWithException

Resumes the execution of the corresponding coroutine so that the exception is re-thrown right after the last suspension point.

fun <T> Continuation<T>.resumeWithException(
    exception: Throwable)
Platform and version requirements: JVM (1.3), JS (1.3), Native (1.3)

startCoroutine

Starts a coroutine without a receiver and with result type T. This function creates and starts a new, fresh instance of suspendable computation every time it is invoked. The completion continuation is invoked when the coroutine completes with a result or an exception.

fun <T> (suspend () -> T).startCoroutine(
    completion: Continuation<T>)

Starts a coroutine with receiver type R and result type T. This function creates and starts a new, fresh instance of suspendable computation every time it is invoked. The completion continuation is invoked when the coroutine completes with a result or an exception.

fun <R, T> (suspend R.() -> T).startCoroutine(
    receiver: R, 
    completion: Continuation<T>)
Platform and version requirements: JVM (1.3), JS (1.3), Native (1.3)

suspendCoroutine

Obtains the current continuation instance inside suspend functions and suspends the currently running coroutine.

suspend fun <T> suspendCoroutine(
    block: (Continuation<T>) -> Unit
): T

© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.coroutines/index.html