-
Notifications
You must be signed in to change notification settings - Fork 529
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
Comments
I'm not knowledgeable enough to determine if there is a specific reason why only I'm a bit worried by the API docs for This is a genuine question, should we follow the advice of the Thanks. |
Here is the /** 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))
()
}
} |
No, that's perfect. Thanks. |
Fix #2236 implement Async.fromThenable (take 2)
js.Thenable
is a super-type ofjs.Promise
The text was updated successfully, but these errors were encountered: