Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IO.fromPromise should accept js.Thenable, instead of js.Promise #2236

Closed
japgolly opened this issue Aug 16, 2021 · 4 comments · Fixed by #2237
Closed

IO.fromPromise should accept js.Thenable, instead of js.Promise #2236

japgolly opened this issue Aug 16, 2021 · 4 comments · Fixed by #2237

Comments

@japgolly
Copy link

js.Thenable is a super-type of js.Promise

@vasilmkd
Copy link
Member

I'm not knowledgeable enough to determine if there is a specific reason why only Promise was implemented and not Thenable.

I'm a bit worried by the API docs for Thenable and Promise, particularly about the fact that then is "inherently un-typeable".

This is a genuine question, should we follow the advice of the Promise scaladoc and use these constructs by making a round-trip through Future or would that be too detrimental for performance. I'm just worried about edge cases that would blow these methods up, but are ultimately out of Cats Effect's power to resolve.

Thanks.

@armanbilge
Copy link
Member

This is a genuine question, should we follow the advice of the Promise scaladoc and use these constructs by making a round-trip through Future or would that be too detrimental for performance

Here is the toFuture implementation in Scala.js and fromPromise in CE. Maybe I'm missing something, but looks like essentially the same code to me.

    /** Converts the [[Thenable]] into a Scala [[scala.concurrent.Future Future]].
     *
     *  Unlike when calling the `then` methods of [[Thenable]], the resulting
     *  [[scala.concurrent.Future Future]] is always properly typed, and
     *  operations on it will be well-typed in turn.
     */
    def toFuture: Future[A] = {
      val p2 = scala.concurrent.Promise[A]()
      p.`then`[Unit](
          { (v: A) =>
            p2.success(v)
            (): Unit | js.Thenable[Unit]
          },
          js.defined { (e: scala.Any) =>
            p2.failure(e match {
              case th: Throwable => th
              case _             => js.JavaScriptException(e)
            })
            (): Unit | js.Thenable[Unit]
          })
      p2.future
    }
  }
  def fromPromise[A](iop: F[Promise[A]]): F[A] =
    flatMap(iop) { p =>
      async_[A] { cb =>
        val onFulfilled: Function1[A, Unit | Thenable[Unit]] =
          (v: A) => cb(Right(v)): Unit | Thenable[Unit]

        val onRejected: Function1[Any, Unit | Thenable[Unit]] = { (a: Any) =>
          val e = a match {
            case th: Throwable => th
            case _ => JavaScriptException(a)
          }

          cb(Left(e)): Unit | Thenable[Unit]
        }

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

        ()
      }
    }

@vasilmkd
Copy link
Member

No, that's perfect. Thanks.

@armanbilge
Copy link
Member

Following revert in #2272, re-opening pending #2273.

@armanbilge armanbilge reopened this Sep 5, 2021
djspiewak added a commit that referenced this issue Sep 8, 2021
Fix #2236 implement Async.fromThenable (take 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants