Frank Sullivan
02/11/2025, 11:01 PM// collected by someone else
return someFlow
.onEach {
launch {
// do the task
}
}
The only thing is, I don't have the current CoroutineScope. I could make a child scope inside onEach with coroutineScope {}
but my understanding is that'll block until the coroutine I'm launching completes.
I could do something like CoroutineScope(coroutineContext).launch
I suppose? But I'm not sure if that's the best practicekevin.cianfarini
02/11/2025, 11:03 PMFrank Sullivan
02/11/2025, 11:07 PMkevin.cianfarini
02/11/2025, 11:19 PMsuspend fun collectTheFlow() = coroutineScope {
flow.onEach {
launch { ... }
}.collect {
...
}
}
kevin.cianfarini
02/11/2025, 11:20 PMFrank Sullivan
02/11/2025, 11:37 PMkevin.cianfarini
02/11/2025, 11:42 PMI could do something likeThis might work okay? But it'd be very weird code and something I'd definitely flag in code review.CoroutineScope(coroutineContext).launch
Frank Sullivan
02/11/2025, 11:47 PMephemient
02/12/2025, 2:49 AMCoroutineScope
is almost equivalent to GlobalScope
ephemient
02/12/2025, 2:57 AMfun <T> Flow<T>.onEachAsync(
action: suspend (T) -> Unit
): Flow<T> = flow {
coroutineScope {
collect {
launch { action(it) }
emit(it)
}
}
}
which would have a scope per flow collectorDaniel Pitts
02/14/2025, 5:40 PMFireAndForgetScope
. Depends on the use-case.Daniel Pitts
02/14/2025, 5:42 PMFrank Sullivan
02/14/2025, 5:43 PMDaniel Pitts
02/14/2025, 5:44 PMsupervisorScope
to keep the failures isolated.