Loading...
Loading...
Kotlin coroutines - structured concurrency, Flows, exception handling
npx skill4agent add pluginagentmarketplace/custom-plugin-kotlin kotlin-coroutines// ✅ Structured - cancellation propagates
class Repository(private val scope: CoroutineScope) {
suspend fun load() = withContext(Dispatchers.IO) { fetch() }
}
// ❌ Avoid GlobalScope
GlobalScope.launch { /* leaks */ }suspend fun loadData() = supervisorScope {
val a = async { fetchA() }
val b = async { fetchB() }
Result(a.awaitOrNull(), b.awaitOrNull())
}
// Never swallow CancellationException
catch (e: Exception) {
if (e is CancellationException) throw e
}@Test
fun test() = runTest {
val vm = ViewModel(testDispatcher)
vm.load()
advanceUntilIdle()
assertThat(vm.state.value.data).isNotNull()
}| Issue | Resolution |
|---|---|
| Coroutine leak | Use structured scopes, not GlobalScope |
| Test hangs | Inject TestDispatcher, use advanceUntilIdle() |
Skill("kotlin-coroutines")