arrow-fx-coroutines / arrow.fx.coroutines / racePair

racePair

suspend fun <A, B> ~~racePair~~(fa: suspend () -> A, fb: suspend () -> B): RacePair<A, B> Deprecated: Will be removed since it leaks Fiber, and breaks structured concurrency. Replace with select

suspend fun <A, B> ~~racePair~~(ctx: CoroutineContext, fa: suspend () -> A, fb: suspend () -> B): RacePair<A, B> Deprecated: Will be removed since it leaks Fiber, and breaks structured concurrency. Replace with select

Races two tasks concurrently within a new suspend fun. Race results in a winner and the other, yet to finish task running in a Fiber.

import arrow.fx.coroutines.*
import arrow.core.Either

suspend fun main(): Unit {
  //sampleStart
  val res = racePair({ never<Int>() }, { "Hello World!" })
  val r = when(res) {
    is Either.Left -> "never cannot win race"
    is Either.Right -> res.b
  }
  //sampleEnd
  println("Race winner result is: $r")
}

Parameters

ctx - CoroutineContext to execute the source fa & fb on.

fa - task to participate in the race

fb - task to participate in the race

Return either Either.Left with product of the winner’s result ƒa and still running task fb, or Either.Right with product of running task ƒa and the winner’s result fb.

See Also

arrow.fx.coroutines.raceN

Do you like Arrow?

Arrow Org
<