Quick Reference

Coroutine Builders

Coroutine Builders

Coroutine builders define ways of starting a coroutine from a suspend lambda and obtaining an object representing its execution.

  val result = coroutineBuilder(prms) {
    ... suspend lambda ...
  }

BuilderReturnDescription

launch

Job

Starts a coroutine from a suspend () -> Unit and returns a Job tracking its execution.

async

Deferred<T>

Starts a coroutine from a suspend () -> T and returns a Deferred<T> tracking its execution.

future

CompletableFuture<T>

Start a coroutine and obtain a CompletableFuture tracking its execution.

runBlocking

T

Starts a coroutine and blocks for its conclusion, returning the resulting value directly.

Coroutine Dispatchers

Coroutine Builders

Coroutine dispatchers define the threading policy for the coroutine execution, i.e., they define the threads where the coroutine continuations are executred. Dispatchers are typically provided as parameters to the coroutine builder.

  val result = coroutineBuilder(coroutineDispatcher) {
    ... suspend lambda ...
  }

DispatcherDescription

CommonPool

Uses the static ForkJoinPool.commonPool thread pool to schedule the coroutine continuations.

Unconfined

Starts the coroutine in the current thread. Continuations are executed in the thread that triggered the coroutine resumption.

UI

Schedules coroutine execution in the Android main UI thread.