Skip to content

Commit

Permalink
Fix typelevel#2236 implement Async.fromThenable
Browse files Browse the repository at this point in the history
  • Loading branch information
armanbilge committed Aug 16, 2021
1 parent 203e25c commit 8e0cc4e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
5 changes: 4 additions & 1 deletion core/js/src/main/scala/cats/effect/IOCompanionPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package cats.effect

import scalajs.js

import scala.scalajs.js.Promise
import scala.scalajs.js.{Promise, Thenable}

private[effect] abstract class IOCompanionPlatform { this: IO.type =>

Expand All @@ -35,6 +35,9 @@ private[effect] abstract class IOCompanionPlatform { this: IO.type =>
apply(thunk)
}

def fromThenable[A](iot: IO[Thenable[A]]): IO[A] =
asyncForIO.fromThenable(iot)

def fromPromise[A](iop: IO[Promise[A]]): IO[A] =
asyncForIO.fromPromise(iop)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import scala.scalajs.js.{|, defined, Function1, JavaScriptException, Promise, Th

private[kernel] trait AsyncPlatform[F[_]] { this: Async[F] =>

def fromPromise[A](iop: F[Promise[A]]): F[A] =
flatMap(iop) { p =>
def fromPromise[A](iop: F[Promise[A]]): F[A] = fromThenable(widen(iop))

def fromThenable[A](iot: F[Thenable[A]]): F[A] =
flatMap(iot) { t =>
async_[A] { cb =>
val onFulfilled: Function1[A, Unit | Thenable[Unit]] =
(v: A) => cb(Right(v)): Unit | Thenable[Unit]
Expand All @@ -35,7 +37,7 @@ private[kernel] trait AsyncPlatform[F[_]] { this: Async[F] =>
cb(Left(e)): Unit | Thenable[Unit]
}

p.`then`[Unit](onFulfilled, defined(onRejected))
t.`then`[Unit](onFulfilled, defined(onRejected))

()
}
Expand Down

0 comments on commit 8e0cc4e

Please sign in to comment.