arrow-fx / arrow.fx / IO / async


fun <A> async(k: IOProc<A>): IO<A>

Create an IO that executes an asynchronous process on evaluation. This combinator can be used to wrap callbacks or other similar impure code that require no cancellation code.

import arrow.core.*
import arrow.fx.*
import java.lang.RuntimeException

typealias Callback = (List<String>?, Throwable?) -> Unit

class GithubId
object GithubService {
  fun getUsernames(callback: Callback) {
    //execute operation and call callback at some point in future

fun main(args: Array<String>) {
  fun getUsernames(): IO<List<String>> =
    IO.async { cb: (Either<Throwable, List<String>>) -> Unit ->
      GithubService.getUsernames { names, throwable ->
        when {
          names != null -> cb(Right(names))
          throwable != null -> cb(Left(throwable))
          else -> cb(Left(RuntimeException("Null result and no exception")))

  val result = getUsernames()


k - an asynchronous computation that might fail typed as IOProc.

See Also



Do you like Arrow?

Arrow Org