//arrow-fx-coroutines/arrow.fx.coroutines/raceN
common
inline suspend fun <A, B> raceN(crossinline fa: suspend
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)
}
either Either.Left if fa won the race, or Either.Right if fb won the race.
common
racePair | for a version that does not automatically cancel the loser. |
raceN | for the same function that can race on any CoroutineContext. |
common
fa | task to participate in the race |
fb | task to participate in the race |
common
inline suspend fun <A, B> raceN(ctx: CoroutineContext = EmptyCoroutineContext, crossinline fa: suspend
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)
}
either Either.Left if fa won the race, or Either.Right if fb won the race.
common
raceN | for a function that ensures it runs in parallel on the Dispatchers.Default. |
common
fa | task to participate in the race |
fb | task to participate in the race |
common
inline suspend fun <A, B, C> raceN(crossinline fa: suspend
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.
common
raceN | for the same function that can race on any CoroutineContext. |
common
inline suspend fun <A, B, C> raceN(ctx: CoroutineContext = EmptyCoroutineContext, crossinline fa: suspend
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.
common
raceN | for a function that ensures operations run in parallel on the Dispatchers.Default. |
Do you like Arrow?
✖