arrow-fx-coroutines / arrow.fx.coroutines / raceN
suspend inline fun <A, B> raceN(crossinline fa: suspend () -> A, crossinline fb: suspend () -> B): Either<A, B>
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.*
suspend fun main(): Unit {
suspend fun loser(): Int =
cancellable { callback ->
// Wait forever and never complete callback
CancelToken { println("Never got cancelled for losing.") }
}
val winner = raceN({ loser() }, { 5 })
val res = when(winner) {
is Either.Left -> "Never always loses race"
is Either.Right -> "Race was won with ${winner.b}"
}
//sampleEnd
println(res)
}
fa
- task to participate in the race
fb
- task to participate in the race
Return either Either.Left if fa won the race, or Either.Right if fb won the race.
See Also
suspend inline fun <A, B> raceN(ctx:
CoroutineContext
= EmptyCoroutineContext, crossinline fa: suspend () -> A, crossinline fb: suspend () -> B): Either<A, B>
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
suspend fun main(): Unit {
suspend fun loser(): Int =
cancellable { callback ->
// Wait forever and never complete callback
CancelToken { println("Never got cancelled for losing.") }
}
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.b}"
}
//sampleEnd
println(res)
}
fa
- task to participate in the race
fb
- task to participate in the race
Return either Either.Left if fa won the race, or Either.Right if fb won the race.
See Also
suspend inline fun <A, B, C> raceN(crossinline fa: suspend () -> A, crossinline fb: suspend () -> B, crossinline fc: suspend () -> C):
Race3
<A, B, C>
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
suspend inline fun <A, B, C> raceN(ctx:
CoroutineContext
= EmptyCoroutineContext, crossinline fa: suspend () -> A, crossinline fb: suspend () -> B, crossinline fc: suspend () -> C):
Race3
<A, B, C>
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
Do you like Arrow?
✖