arrow-effects-extensions / arrow.effects.extensions.io.async / asyncF
@JvmName("asyncF") fun <A> asyncF(arg0: ((Either<
Throwable
,
A
>) ->
Unit
) -> Kind<ForIO,
Unit
>): IO<
A
>
async variant that can suspend side effects in the provided registration function.
The passed in function is injected with a side-effectful callback for signaling the final result of an asynchronous process.
import arrow.effects.*
import arrow.effects.extensions.io.async.*
import arrow.core.*
import arrow.effects.*
import arrow.effects.typeclasses.Async
fun main(args: Array<String>) {
//sampleStart
fun <F> Async<F>.makeCompleteAndGetPromiseInAsync() =
asyncF<String> { cb: (Either<Throwable, String>) -> Unit ->
Promise.uncancelable<F, String>(this).flatMap { promise ->
promise.complete("Hello World!").flatMap {
promise.get().map { str -> cb(Right(str)) }
}
}
}
val result = IO.async().makeCompleteAndGetPromiseInAsync()
//sampleEnd
println(result)
}
See Also