arrow-fx / arrow.fx / Semaphore / withPermit

withPermit

abstract fun <A> withPermit(t: Kind<F, A>): Kind<F, A>

Runs the supplied effect that acquires a permit, and then releases the permit.

import arrow.fx.*
import arrow.fx.extensions.io.async.async
import arrow.fx.extensions.io.monad.flatMap

fun main(args: Array<String>) {
  //sampleStart
  val semaphore = Semaphore.uncancellable<ForIO>(5, IO.async())

  val result = semaphore.flatMap { s ->
    s.withPermit(IO { "Use controlled resource" })
  }.unsafeRunSync()
  //sampleEnd
  println(result)
}

Do you like Arrow?

Arrow Org
<