Skip to content

Instantly share code, notes, and snippets.

@jdegoes jdegoes/AsyncToIO.scala
Last active Nov 11, 2017

Embed
What would you like to do?
A sketch of an `Async ~> IO`
val AsyncToIO: NaturalTransformation[Async, IO] {
def apply[A](fa: Async[A]): IO[A] = {
for {
ref <- newIORef[Either[Throwable, A]](Left(new Error("No value")))
counter <- IO(new java.util.concurrent.CountDownLatch(1))
_ <- fa.register(v => ref.set(v).flatMap(_ => IO(counter.countDown()))
_ <- IO(counter.await())
v <- ref.get
a <- v match {
case Left(e) => IO.fail(e)
case Right(a) => IO(a)
}
} yield a
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.