arrow-fx-stm / arrow.fx.stm / STM / take

take

open fun <A> TMVar<A>.take(): A

Read the value from a TMVar and empty it.

import arrow.fx.stm.TMVar
import arrow.fx.stm.atomically

suspend fun main() {
  //sampleStart
  val tmvar = TMVar.new(10)
  val result = atomically {
    tmvar.take()
  }
  //sampleEnd
  println("Result $result")
  println("New value ${atomically { tmvar.tryTake() } }")
}

This retries if the TMVar is empty and leaves the TMVar empty if it succeeded.

See Also

TMVar.tryTake

TMVar.read

Do you like Arrow?

Arrow Org
<