//arrow-fx-coroutines/arrow.fx.coroutines/raceN

raceN

common inline suspend fun <A, B> raceN(crossinline fa: suspend .() -> [A](race-n.html), crossinline fb: suspend .() -> [B](race-n.html)): <[A](race-n.html), [B](race-n.html)>

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

common

   
racePair for a version that does not automatically cancel the loser.
raceN for the same function that can race on any CoroutineContext.

Parameters

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 .() -> [A](race-n.html), crossinline fb: suspend .() -> [B](race-n.html)): <[A](race-n.html), [B](race-n.html)>

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

common

   
raceN for a function that ensures it runs in parallel on the Dispatchers.Default.

Parameters

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 .() -> [A](race-n.html), crossinline fb: suspend .() -> [B](race-n.html), crossinline fc: suspend .() -> [C](race-n.html)): [Race3](-race3/index.html)<[A](race-n.html), [B](race-n.html), [C](race-n.html)>

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

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 .() -> [A](race-n.html), crossinline fb: suspend .() -> [B](race-n.html), crossinline fc: suspend .() -> [C](race-n.html)): [Race3](-race3/index.html)<[A](race-n.html), [B](race-n.html), [C](race-n.html)>

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

common

   
raceN for a function that ensures operations run in parallel on the Dispatchers.Default.

Do you like Arrow?

Arrow Org
<