arrow-fx / arrow.fx.typeclasses / Concurrent / waitFor

waitFor

open fun <A> Kind<F, A>.waitFor(duration: Duration, default: Kind<F, A>): Kind<F, A>

Returns the result of this within the specified duration or the default value.

import arrow.*
import arrow.fx.*
import arrow.fx.typeclasses.*
import arrow.fx.extensions.io.concurrent.concurrent

fun main(args: Array<String>) {
  //sampleStart
  fun <F> Concurrent<F>.timedOutWorld(): Kind<F, Unit> {
    val world = sleep(3.seconds).flatMap { effect { println("Hello World!") } }
    val fallbackWorld = effect { println("Hello from the backup") }
    return world.waitFor(1.seconds, fallbackWorld)
  }
  //sampleEnd
  IO.concurrent().timedOutWorld()
    .fix().unsafeRunSync()
}

open fun <A> Kind<F, A>.waitFor(duration: Duration): Kind<F, A>

Returns the result of this within the specified duration or the raises a TimeoutException exception.

import arrow.*
import arrow.fx.*
import arrow.fx.typeclasses.*
import arrow.fx.extensions.io.concurrent.concurrent

fun main(args: Array<String>) {
  //sampleStart
  fun <F> Concurrent<F>.timedOutWorld(): Kind<F, Unit> {
    val world = sleep(1.seconds).flatMap { effect { println("Hello World!") } }
    return world.waitFor(3.seconds)
  }
  //sampleEnd
  IO.concurrent().timedOutWorld()
    .fix().unsafeRunSync()
}

Do you like Arrow?

Arrow Org
<