raceN

inline suspend fun <A, B> raceN(crossinline fa: suspend CoroutineScope.() -> A, crossinline fb: suspend CoroutineScope.() -> B): Either<A, B>(source)

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

racePair

for a version that does not automatically cancel the loser.

for the same function that can race on any CoroutineContext.

Parameters

fa

task to participate in the race

fb

task to participate in the race


inline suspend fun <A, B> raceN(ctx: CoroutineContext = EmptyCoroutineContext, crossinline fa: suspend CoroutineScope.() -> A, crossinline fb: suspend CoroutineScope.() -> B): Either<A, B>(source)

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

fa

task to participate in the race

fb

task to participate in the race


inline suspend fun <A, B, C> raceN(crossinline fa: suspend CoroutineScope.() -> A, crossinline fb: suspend CoroutineScope.() -> B, crossinline fc: suspend CoroutineScope.() -> C): Race3<A, B, C>(source)

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.


inline suspend fun <A, B, C> raceN(ctx: CoroutineContext = EmptyCoroutineContext, crossinline fa: suspend CoroutineScope.() -> A, crossinline fb: suspend CoroutineScope.() -> B, crossinline fc: suspend CoroutineScope.() -> C): Race3<A, B, C>(source)

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.