{"id":"2d045d89-3b1d-4bf1-a4fe-d99daa19244c","shortId":"Wrstw9","kind":"skill","title":"kotlin-coroutines-expert","tagline":"Expert patterns for Kotlin Coroutines and Flow, covering structured concurrency, error handling, and testing.","description":"# 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.\n\n## Limitations\n- Use this skill only when the task clearly matches the scope described above.\n- Do not treat the output as a substitute for environment-specific validation, testing, or expert review.\n- Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.","tags":["kotlin","coroutines","expert","antigravity","awesome","skills","sickn33","agent-skills","agentic-skills","ai-agent-skills","ai-agents","ai-coding"],"capabilities":["skill","source-sickn33","skill-kotlin-coroutines-expert","topic-agent-skills","topic-agentic-skills","topic-ai-agent-skills","topic-ai-agents","topic-ai-coding","topic-ai-workflows","topic-antigravity","topic-antigravity-skills","topic-claude-code","topic-claude-code-skills","topic-codex-cli","topic-codex-skills"],"categories":["antigravity-awesome-skills"],"synonyms":[],"warnings":[],"endpointUrl":"https://skills.sh/sickn33/antigravity-awesome-skills/kotlin-coroutines-expert","protocol":"skill","transport":"skills-sh","auth":{"type":"none","details":{"cli":"npx skills add sickn33/antigravity-awesome-skills","source_repo":"https://github.com/sickn33/antigravity-awesome-skills","install_from":"skills.sh"}},"qualityScore":"0.700","qualityRationale":"deterministic score 0.70 from registry signals: · indexed on github topic:agent-skills · 34726 github stars · SKILL.md body (3,149 chars)","verified":false,"liveness":"unknown","lastLivenessCheck":null,"agentReviews":{"count":0,"score_avg":null,"cost_usd_avg":null,"success_rate":null,"latency_p50_ms":null,"narrative_summary":null,"summary_updated_at":null},"enrichmentModel":"deterministic:skill-github:v1","enrichmentVersion":1,"enrichedAt":"2026-04-23T12:51:08.053Z","embedding":null,"createdAt":"2026-04-18T21:39:35.139Z","updatedAt":"2026-04-23T12:51:08.053Z","lastSeenAt":"2026-04-23T12:51:08.053Z","tsv":"'1':87,209 '2':124 '3':165 '300':193 'advanc':33 'alway':90 'api.fetcha':224 'api.fetchb':232 'ask':360 'async':113,117,222,231 'asynchron':27,53 'best':251 'block':257 'boundari':368 'break':285 'cancel':69,239,261 'cancellationexcept':296 'catch':139,158,225,295 'caught':152 'clarif':362 'class':320 'clear':335 'cold':184 'concurr':14,37,89,103,287 'control':145,324 'coroutin':3,9,20,31,68,92,279,303 'coroutineexceptionhandl':128,149 'coroutinescop':96,98,110 'cover':12,32 'criteria':371 'dashboarddata':109,119 'data':61 'debounc':192 'debug':67 'defin':95 'describ':339 'design':59 'dispatchers.io':199,255 'e':159,226 'e.g':269 'ensur':310 'environ':351 'environment-specif':350 'error':15,163,213 'event':182 'exampl':207,208 'except':40,71,125,150,153,227 'execut':211 'expert':4,5,21,356 'fail':235,307 'fetchdatawitherrorhandl':218 'flatmaplatest':194 'flow':11,38,64,81,169,185,189,201 'flowon':198 'fun':107,217 'function':79,142 'globalscop':283 'granular':144 'group':102 'guid':24,86 'handl':16,41,126,161,214 'handler':148,155 'hang':305 'hot':200 'i/o':258 'implement':52 'inject':316 'input':365 'ioexcept':160 'kotlin':2,8,19,30,56,105,146,183,215 'kotlin-coroutines-expert':1 'launch':91 'lazi':186 'lead':290 'leak':292 'level':132 'like':35 'limit':327 'list':190 'loaddashboarddata':108 'longer':267 'master':26 'match':336 'may':249 'miss':373 'need':175,268 'network':162 'null':228 'oper':54,259 'output':345 'overview':22 'parallel':210 'pattern':6 'permiss':366 'practic':252 'println':151 'problem':302 'program':28 'queri':195,197 'reactiv':60,166 'reli':135 'requir':364 'result1':244 'result2':247 'retain':178 'rethrow':299 'review':357 'riskyoper':157 'runtest':275,314 'safeti':367 'scope':133,262,338 'searchqueri':191 'searchrepo.search':196 'searchresult':188 'set':122 'settingsdef':116 'settingsdeferred.await':123 'settingsrepo.getsettings':118 'sharedflow':180 'skill':49,330 'skill-kotlin-coroutines-expert' 'solut':309 'source-sickn33' 'specif':164,352 'state':173,202 'stateflow':171,205 'step':83,85 'step-by-step':82 'stop':358 'strategi':44 'stream':62,167 'structur':13,36,88,286 'substitut':348 'success':370 'supervisorscop':100,219,242 'suspend':78,106,141,216 'task':104,334 'task1':221,236 'task1.await':245 'task2':230,234 'task2.await':248 'test':18,43,76,278,304,354 'testdispatch':317 'testscop':273 'throw':250 'time':326 'top':131 'top-level':130 'topic':34 'topic-agent-skills' 'topic-agentic-skills' 'topic-ai-agent-skills' 'topic-ai-agents' 'topic-ai-coding' 'topic-ai-workflows' 'topic-antigravity' 'topic-antigravity-skills' 'topic-claude-code' 'topic-claude-code-skills' 'topic-codex-cli' 'topic-codex-skills' 'transform':39 'treat':343 'tri':138,156,223 'troubleshoot':301 'try-catch':137 'uistat':204 'uistate.asstateflow':206 'unit':75,277 'unless':297 'unpredict':308 'use':47,50,57,65,72,97,127,170,254,272,282,313,328 'user':120 'userdef':112 'userdeferred.await':121 'userrepo.getuser':114 'val':111,115,147,187,203,220,229,243,246 'valid':353 'viewmodel.oncleared':270 'viewmodelscope.launch':154 'virtual':325 'within':93,140 'write':74","prices":[{"id":"9a8236cc-899c-4d76-a49c-3e6aff6f687c","listingId":"2d045d89-3b1d-4bf1-a4fe-d99daa19244c","amountUsd":"0","unit":"free","nativeCurrency":null,"nativeAmount":null,"chain":null,"payTo":null,"paymentMethod":"skill-free","isPrimary":true,"details":{"org":"sickn33","category":"antigravity-awesome-skills","install_from":"skills.sh"},"createdAt":"2026-04-18T21:39:35.139Z"}],"sources":[{"listingId":"2d045d89-3b1d-4bf1-a4fe-d99daa19244c","source":"github","sourceId":"sickn33/antigravity-awesome-skills/kotlin-coroutines-expert","sourceUrl":"https://github.com/sickn33/antigravity-awesome-skills/tree/main/skills/kotlin-coroutines-expert","isPrimary":false,"firstSeenAt":"2026-04-18T21:39:35.139Z","lastSeenAt":"2026-04-23T12:51:08.053Z"}],"details":{"listingId":"2d045d89-3b1d-4bf1-a4fe-d99daa19244c","quickStartSnippet":null,"exampleRequest":null,"exampleResponse":null,"schema":null,"openapiUrl":null,"agentsTxtUrl":null,"citations":[],"useCases":[],"bestFor":[],"notFor":[],"kindDetails":{"org":"sickn33","slug":"kotlin-coroutines-expert","github":{"repo":"sickn33/antigravity-awesome-skills","stars":34726,"topics":["agent-skills","agentic-skills","ai-agent-skills","ai-agents","ai-coding","ai-workflows","antigravity","antigravity-skills","claude-code","claude-code-skills","codex-cli","codex-skills","cursor","cursor-skills","developer-tools","gemini-cli","gemini-skills","kiro","mcp","skill-library"],"license":"mit","html_url":"https://github.com/sickn33/antigravity-awesome-skills","pushed_at":"2026-04-23T06:41:03Z","description":"Installable GitHub library of 1,400+ agentic skills for Claude Code, Cursor, Codex CLI, Gemini CLI, Antigravity, and more. Includes installer CLI, bundles, workflows, and official/community skill collections.","skill_md_sha":"9b8a90170801241d808a5ad4597e3ecd8d03bac0","skill_md_path":"skills/kotlin-coroutines-expert/SKILL.md","default_branch":"main","skill_tree_url":"https://github.com/sickn33/antigravity-awesome-skills/tree/main/skills/kotlin-coroutines-expert"},"layout":"multi","source":"github","category":"antigravity-awesome-skills","frontmatter":{"name":"kotlin-coroutines-expert","description":"Expert patterns for Kotlin Coroutines and Flow, covering structured concurrency, error handling, and testing."},"skills_sh_url":"https://skills.sh/sickn33/antigravity-awesome-skills/kotlin-coroutines-expert"},"updatedAt":"2026-04-23T12:51:08.053Z"}}