Skip to content
Commitwise
Menu

Coroutine

A function that can pause and resume later without blocking a thread, used for async code in Kotlin.

Definition

A coroutine can suspend at a suspend call, for example while waiting for the network, and free its thread for other work. When the result arrives, it resumes where it left off. Thousands of coroutines can share a handful of threads, and structured concurrency (scopes) makes sure they are cancelled together.

In practice

viewModelScope.launch { val user = api.loadUser() } loads a user without freezing the UI, and is cancelled automatically when the screen's ViewModel is cleared.

Learn it in context

Lessons that use this term.

Related terms

Esc

Type to search the whole site.