raceN
Races the participants fa, fb in parallel on the Dispatchers.Default. The winner of the race cancels the other participants. Cancelling the operation cancels all participants. An uncancellable participant will back-pressure the result of raceN.
import arrow.core.Either
import arrow.fx.coroutines.*
import kotlinx.coroutines.suspendCancellableCoroutine
suspend fun main(): Unit {
suspend fun loser(): Int =
guaranteeCase({ never() }) { exitCase ->
println("I can never win the race. Finished with $exitCase.")
}
val winner = raceN({ loser() }, { 5 })
val res = when(winner) {
is Either.Left -> "Never always loses race"
is Either.Right -> "Race was won with ${winner.value}"
}
//sampleEnd
println(res)
}
Return
either Either.Left if fa won the race, or Either.Right if fb won the race.
See also
for a version that does not automatically cancel the loser.
for the same function that can race on any CoroutineContext.
Parameters
task to participate in the race
task to participate in the race
Races the participants fa, fb on the provided CoroutineContext. The winner of the race cancels the other participants. Cancelling the operation cancels all participants.
Coroutine context is inherited from a CoroutineScope, additional context elements can be specified with ctx argument. If the combined context does not have any dispatcher nor any other ContinuationInterceptor, then Dispatchers.Default is used. WARNING If the combined context has a single threaded ContinuationInterceptor, this function will not run fa&fb in parallel.
import arrow.core.Either
import arrow.fx.coroutines.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.suspendCancellableCoroutine
suspend fun main(): Unit {
suspend fun loser(): Int =
guaranteeCase({ never() }) { exitCase ->
println("I can never win the race. Finished with $exitCase.")
}
val winner = raceN(Dispatchers.IO, { loser() }, { 5 })
val res = when(winner) {
is Either.Left -> "Never always loses race"
is Either.Right -> "Race was won with ${winner.value}"
}
//sampleEnd
println(res)
}
Return
either Either.Left if fa won the race, or Either.Right if fb won the race.
See also
for a function that ensures it runs in parallel on the Dispatchers.Default.
Parameters
task to participate in the race
task to participate in the race
Races the participants fa, fb&fc in parallel on the Dispatchers.Default. The winner of the race cancels the other participants. Cancelling the operation cancels all participants.
See also
for the same function that can race on any CoroutineContext.
Races the participants fa, fb&fc on the provided CoroutineContext. The winner of the race cancels the other participants. Cancelling the operation cancels all participants.
Coroutine context is inherited from a CoroutineScope, additional context elements can be specified with ctx argument. If the combined context does not have any dispatcher nor any other ContinuationInterceptor, then Dispatchers.Default is used. WARNING If the combined context has a single threaded ContinuationInterceptor, this function will not run fa, fb&fc in parallel.
See also
for a function that ensures operations run in parallel on the Dispatchers.Default.