arrow-fx-coroutines / arrow.fx.coroutines / Semaphore / withPermitN

withPermitN

abstract suspend fun <A> withPermitN(n: Long, fa: suspend () -> A): A

Runs the supplied effect with an acquired permit, and releases the permit on ExitCase.

import arrow.fx.coroutines.*

suspend fun main(): Unit {
  //sampleStart
  val semaphore = Semaphore(5)
  val available = semaphore.withPermitN(4) {
    println("I'll run after I got 4 permits first")
    semaphore.available()
  }
  //sampleEnd
  println("available: $available")
}

Do you like Arrow?

Arrow Org
<