Kotlin Coroutines Expert
SkillAI & modelskotlin-coroutines-expert is a skill that gives an AI agent expert patterns for Kotlin Coroutines and Flow. It covers structured concurrency, error handling, and testing, so the agent writes correct coroutine and Flow code with proper structure and tests. It is a kotlin claude skill for developers working with asynchronous Kotlin.
Available today. Use it from your connected AI after setup.
No other account needed.
Have an AI agent that supports loading skills.
Then ask your AI: use the Kotlin Coroutines Expert skill
What your AI can do with it
- Applies expert patterns for Kotlin Coroutines
- Applies expert patterns for Kotlin Flow
- Guides structured concurrency in coroutine code
- Handles error handling patterns for coroutines and Flow
- Supports testing of coroutine and Flow code
Getting started
- Have an AI agent that supports loading skills.
- Add the kotlin-coroutines-expert skill to the agent's available skills.
- Ask the agent to write or review Kotlin Coroutines or Flow code, and it will apply the skill's patterns.
What this skill tells your AI
The instructions your AI receives, as published by davila7/claude-code-templates in cli-tool/components/skills/development/kotlin-coroutines-expert/SKILL.md and read by ahel’s review.
Overview
A guide to mastering asynchronous programming with Kotlin Coroutines. Covers advanced topics like structured concurrency, Flow transformations, exception handling, and testing strategies.
When to Use This Skill
- Use when implementing asynchronous operations in Kotlin.
- Use when designing reactive data streams with
Flow. - Use when debugging coroutine cancellations or exceptions.
- Use when writing unit tests for suspending functions or Flows.
Step-by-Step Guide
1. Structured Concurrency
Always launch coroutines within a defined CoroutineScope. Use coroutineScope or supervisorScope to group concurrent tasks.
suspend fun loadDashboardData(): DashboardData = coroutineScope {
val userDeferred = async { userRepo.getUser() }
val settingsDeferred = async { settingsRepo.getSettings() }
DashboardData(
user = userDeferred.await(),
settings = settingsDeferred.await()
)
}
2. Exception Handling
Use CoroutineExceptionHandler for top-level scopes, but rely on try-catch within suspending functions for granular control.
val handler = CoroutineExceptionHandler { _, exception ->
println("Caught $exception")
}
viewModelScope.launch(handler) {
try {
riskyOperation()
} catch (e: IOException) {
// Handle network error specifically
}
}
3. Reactive Streams with Flow
Use StateFlow for state that needs to be retained, and SharedFlow for events.
// Cold Flow (Lazy)
val searchResults: Flow<List<Item>> = searchQuery
.debounce(300)
.flatMapLatest { query -> searchRepo.search(query) }
.flowOn(Dispatchers.IO)
// Hot Flow (State)
val uiState: StateFlow<UiState> = _uiState.asStateFlow()
Examples
Example 1: Parallel Execution with Error Handling
suspend fun fetchDataWithErrorHandling() = supervisorScope {
val task1 = async {
try { api.fetchA() } catch (e: Exception) { null }
}
val task2 = async { api.fetchB() }
// If task2 fails, task1 is NOT cancelled because of supervisorScope
val result1 = task1.await()
val result2 = task2.await() // May throw
}
Best Practices
- ✅ Do: Use
Dispatchers.IOfor blocking I/O operations. - ✅ Do: Cancel scopes when they are no longer needed (e.g.,
ViewModel.onCleared). - ✅ Do: Use
TestScopeandrunTestfor unit testing coroutines. - ❌ Don't: Use
GlobalScope. It breaks structured concurrency and can lead to leaks. - ❌ Don't: Catch
CancellationExceptionunless you rethrow it.
Troubleshooting
Problem: Coroutine test hangs or fails unpredictably.
Solution: Ensure you are using runTest and injecting TestDispatcher into your classes so you can control virtual time.
Signals
- GitHub stars
- 32k
- Forks
- 4k
- Last commit
- Sep 2026
Questions
- What does this skill do?
- It gives an AI agent expert patterns for Kotlin Coroutines and Flow, covering structured concurrency, error handling, and testing, so the agent writes correct coroutine and Flow code with proper structure and tests.
- Do I need to know Kotlin Coroutines to use it?
- No. The skill supplies the expert patterns; you just need an agent that can load skills and Kotlin code you want written or reviewed.
- Does it help with testing coroutines?
- Yes. Testing is one of the areas the skill covers, alongside structured concurrency and error handling.
Advanced
- Item type
- skill
- Key
kotlin-coroutines-expert- Source
- github.com/davila7/claude-code-templates