{
  "schema": "https://ai-atoms.com/schemas/skill-v1.json",
  "type": "skill",
  "id": "skill/kotlin-coroutines-expert",
  "version": "1.0.0",
  "name": "Kotlin Coroutines Expert",
  "description": "Expert patterns for Kotlin Coroutines and Flow, covering structured concurrency, error handling, and testing.",
  "system_prompt_fragment": "# Kotlin Coroutines Expert\n\n## Overview\n\nA guide to mastering asynchronous programming with Kotlin Coroutines. Covers advanced topics like structured concurrency, `Flow` transformations, exception handling, and testing strategies.\n\n## When to Use This Skill\n\n- Use when implementing asynchronous operations in Kotlin.\n- Use when designing reactive data streams with `Flow`.\n- Use when debugging coroutine cancellations or exceptions.\n- Use when writing unit tests for suspending functions or Flows.\n\n## Step-by-Step Guide\n\n### 1. Structured Concurrency\n\nAlways launch coroutines within a defined `CoroutineScope`. Use `coroutineScope` or `supervisorScope` to group concurrent tasks.\n\n```kotlin\nsuspend fun loadDashboardData(): DashboardData = coroutineScope {\n    val userDeferred = async { userRepo.getUser() }\n    val settingsDeferred = async { settingsRepo.getSettings() }\n    \n    DashboardData(\n        user = userDeferred.await(),\n        settings = settingsDeferred.await()\n    )\n}\n```\n\n### 2. Exception Handling\n\nUse `CoroutineExceptionHandler` for top-level scopes, but rely on `try-catch` within suspending functions for granular control.\n\n```kotlin\nval handler = CoroutineExceptionHandler { _, exception ->\n    println(\"Caught $exception\")\n}\n\nviewModelScope.launch(handler) {\n    try {\n        riskyOperation()\n    } catch (e: IOException) {\n        // Handle network error specifically\n    }\n}\n```\n\n### 3. Reactive Streams with Flow\n\nUse `StateFlow` for state that needs to be retained, and `SharedFlow` for events.\n\n```kotlin\n// Cold Flow (Lazy)\nval searchResults: Flow<List<Item>> = searchQuery\n    .debounce(300)\n    .flatMapLatest { query -> searchRepo.search(query) }\n    .flowOn(Dispatchers.IO)\n\n// Hot Flow (State)\nval uiState: StateFlow<UiState> = _uiState.asStateFlow()\n```\n\n## Examples\n\n### Example 1: Parallel Execution with Error Handling\n\n```kotlin\nsuspend fun fetchDataWithErrorHandling() = supervisorScope {\n    val task1 = async { \n        try { api.fetchA() } catch (e: Exception) { null } \n    }\n    val task2 = async { api.fetchB() }\n    \n    // If task2 fails, task1 is NOT cancelled because of supervisorScope\n    val result1 = task1.await()\n    val result2 = task2.await() // May throw\n}\n```\n\n## Best Practices\n\n- ✅ **Do:** Use `Dispatchers.IO` for blocking I/O operations.\n- ✅ **Do:** Cancel scopes when they are no longer needed (e.g., `ViewModel.onCleared`).\n- ✅ **Do:** Use `TestScope` and `runTest` for unit testing coroutines.\n- ❌ **Don't:** Use `GlobalScope`. It breaks structured concurrency and can lead to leaks.\n- ❌ **Don't:** Catch `CancellationException` unless you rethrow it.\n\n## Troubleshooting\n\n**Problem:** Coroutine test hangs or fails unpredictably.\n**Solution:** Ensure you are using `runTest` and injecting `TestDispatcher` into your classes so you can control virtual time.",
  "applicable_domains": [
    "other"
  ],
  "category": "other",
  "invocation": [
    "/kotlin-coroutines-expert"
  ],
  "authored_by": "claudeskills.in community",
  "source_url": "https://claudeskills.in/skill/kotlin-coroutines-expert",
  "provenance": {
    "source": "claudeskills.in",
    "source_url": "https://claudeskills.in/skill/kotlin-coroutines-expert",
    "license": "unknown",
    "imported_at": "2026-09-03",
    "notes": "Aggregated by claudeskills.in from community GitHub lists."
  },
  "tags": [
    "claudeskills",
    "other",
    "risk-reviewed"
  ],
  "lifecycle": "draft"
}