Davio
07/31/2024, 6:59 AMSam
07/31/2024, 7:49 AMfun <T> race(vararg tasks: suspend () -> T) = channelFlow {
for (task in tasks) launch { send(task()) }
}
race(task1, task2).filterNotNull().first()
Davio
07/31/2024, 8:04 AMSam
07/31/2024, 8:05 AMfirst()
is a terminal operator that will end the flow. The channel flow has its own coroutine scope which is cancelled when the flow terminates.Davio
07/31/2024, 8:18 AM