kotlinx-coroutines

Guide developers in writing asynchronous Kotlin code with coroutines.

Updated Apr 8, 2026
One-click install
npx skills add https://github.com/ClankerGuru/opsx --skill kotlinx-coroutines
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: kotlinx-coroutines
Source: https://github.com/ClankerGuru/opsx/tree/main/cli/src/main/resources/content/skills/kotlinx-coroutines
Command: npx skills add https://github.com/ClankerGuru/opsx --skill kotlinx-coroutines

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Writing asynchronous Kotlin code is error-prone when relying on callbacks or manual thread management; this guide consolidates best practices for safe, readable concurrency using coroutines.

Core Features & Use Cases

  • Structured concurrency: ensure a parent scope waits for all children, with proper cancellation propagation.
  • Suspend functions, dispatchers, cancellation, Flow and StateFlow/SharedFlow for streaming data and reactive UI patterns.
  • Testing with runTest and coroutine-aware testing patterns to achieve deterministic time control and reliable assertions.

Quick Start

Create a small Kotlin project and implement a simple coroutine example to practice structured concurrency.

Frequently Asked Questions about kotlinx-coroutines

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write reliable async Kotlin code with structured concurrency?

Structured concurrency in Kotlin ensures a parent coroutine scope waits for all children to complete, automatically propagating cancellation downward. This prevents memory leaks by guaranteeing no child coroutine outlives its parent scope.

What's the best way to handle streaming data in Kotlin coroutines?

Use Kotlin Flow for reactive streaming data. StateFlow and SharedFlow enable reactive UI patterns by emitting multiple values over time, replacing manual callback management with structured, cancellable data streams.

How does coroutine cancellation work in Kotlin?

Coroutine cancellation propagates through structured concurrency, stopping child jobs when a parent cancels. Suspend functions cooperate by checking cancellation status, ensuring clean resource cleanup and preventing orphaned background tasks.

Can I use Kotlin coroutines across JVM and Android platforms?

Kotlin coroutines work across JVM and Android. Dispatchers control thread execution context, routing coroutine work to appropriate thread pools for UI updates or heavy background computation on either platform.

How do I test suspend functions and Flow with deterministic time control?

Use runTest for testing Kotlin coroutines with deterministic time control. This coroutine-aware testing pattern replaces real delays with virtual time, ensuring reliable assertions for suspend functions and Flow emissions.

Why do my Kotlin coroutines leak memory or run indefinitely?

Coroutines leak memory without structured concurrency, as orphaned jobs keep running without parent oversight. Properly scoped coroutines enforce cancellation propagation, ensuring all child jobs terminate when their parent scope ends.