Kotlin Coroutines taste
# Kotlin Coroutines taste of an Android and JVM engineer who wants structured concurrency everywhere - Launch coroutines only from a scope tied to a lifecycle, such as viewModelScope, lifecycleScope or a scope owned
Works with
claude-code (compatible), claude (compatible), codex (compatible), chatgpt (compatible), gemini-cli (compatible), cursor (compatible), copilot (compatible), opencode (compatible), amp (compatible), roo (compatible), kiro (compatible), goose (compatible), junie (compatible), trae (compatible), cline (compatible), continue (compatible), antigravity (compatible), windsurf (compatible), augment (compatible), qwen-code (compatible), aider (compatible)
Content
Kotlin Coroutines taste of an Android and JVM engineer who wants structured concurrency everywhere
- Launch coroutines only from a scope tied to a lifecycle, such as
viewModelScope,lifecycleScopeor a scope owned by a service. Confidence: 0.95 - Never use
GlobalScope; if work must outlive a screen, hand it to an application-level scope or WorkManager. Confidence: 0.95 - Make suspend functions main-safe: the function itself switches to
Dispatchers.IOorDefaultwithwithContext, not the caller. Confidence: 0.90 - Inject dispatchers through the constructor so tests can replace them with a
TestDispatcher. Confidence: 0.90 - Use
coroutineScopefor parallel work that should fail together, andsupervisorScopeonly when children must fail independently. Confidence: 0.85 - Never swallow
CancellationException; rethrow it from every broadcatchblock. Confidence: 0.95 - Prefer
async/awaitonly for real parallel decomposition, not as a replacement for a plain suspend call. Confidence: 0.80 - Expose UI state as
StateFlowand one-off events through aChannelor a state field that the UI consumes. Confidence: 0.80 - Collect flows in the UI with
repeatOnLifecycle(Lifecycle.State.STARTED)orcollectAsStateWithLifecycle. Confidence: 0.90 - Use
stateInwithSharingStarted.WhileSubscribed(5_000)so upstream work stops when nobody is watching. Confidence: 0.80 - Keep flows cold by default and convert to hot with
shareInorstateInonly at a clear boundary. Confidence: 0.80 - Apply
flowOnto move upstream work off the main thread instead of wrapping collectors inwithContext. Confidence: 0.80 - Wrap blocking Java APIs in
withContext(Dispatchers.IO)and never callrunBlockingin production code paths. Confidence: 0.90 - Add timeouts to network and database calls with
withTimeoutorwithTimeoutOrNull, not hand-rolled delays. Confidence: 0.75 - Check
ensureActive()oryield()inside long CPU loops so cancellation actually stops the work. Confidence: 0.80 - Protect shared mutable state with a
Mutexor confine it to a single coroutine, never a plainsynchronizedblock around a suspend call. Confidence: 0.85 - Install a
CoroutineExceptionHandlerat the root scope to log crashes, but handle expected errors withResultor sealed types. Confidence: 0.75 - Test coroutines with
runTestand virtual time, and test flows with Turbine. Confidence: 0.85 - Name important coroutines with
CoroutineNameso logs and debugger output stay readable. Confidence: 0.65 - Keep suspend functions free of
launchside effects; a function that starts background work should say so in its name. Confidence: 0.75
Source and license
- Source
- Prompts Chat
- Original
- f/awesome-chatgpt-prompts/prompts.csv#kotlin-coroutines-taste
- License
- MIT