斯卡拉的未来.js
Futures in Scala.js
我尝试在 Scala 中使用 Promise and Futures.js。承诺有效,一旦涉及到期货,我就会收到警告和错误。
尝试:
val p1 = Promise[Int]
val f1: Future[Int] = p1.future
val p2 = Promise[Int]
val f2: Future[Int] = p2.future
val res1 = for {
v1 <- f1
v2 <- f2
} yield v1 + v2
val res2 = f1.flatMap(x => f2.map(y => x + y))
res1 onSuccess {
case x: Int => g.console.log(x);
}
res2 onSuccess {
case x: Int => g.console.log(x);
}
// callback in dom, using ScalaTags
// div(`class` := "btn btn-default", `type` := "button", onclick := click(1, p1))
def click(i: Int, p: Promise[Int])(x: dom.MouseEvent): Unit = {
g.console.log(i);
try {
p success i
}
catch {
case x: Throwable => println("again")
}
}
f1 onSuccess {
case x: Int => 1
}
我进入了sbt fastOptJs:
[warn] Referring to non-existent class jl_Thread$UncaughtExceptionHandler
[warn] called from s_concurrent_impl_ExecutionContextImpl.init___ju_concurrent_Executor__F1
[warn] called from s_concurrent_impl_ExecutionContextImpl$.fromExecutor__ju_concurrent_Executor__F1__s_concurrent_impl_ExecutionContextImpl
[warn] called from s_concurrent_ExecutionContext$Implicits$.global$lzycompute__p1__s_concurrent_ExecutionContextExecutor
[warn] called from s_concurrent_ExecutionContext$Implicits$.global__s_concurrent_ExecutionContextExecutor
[warn] called from Lexample_H2$class.Lexample_H2$class__$init$__Lexample_H2__V
[warn]
我进入浏览器:
uncaught exception: java.lang.RuntimeException: System.getProperty() not implemented
缺少/未实施什么?我该如何实现它?有解决方法吗?如何实现一个对处理浏览器中的事件有意义的 ExecutionContext?
从 Scala.js
0.6.0 开始,Scala 的标准global
ExecutionContext
在 Scala.js 中可用。您可以通过以下方式导入它
import scala.concurrent.ExecutionContext.Implicits.global
// now you get to play with Futures
在 Scala.js 中,它是 scala.scalajs.concurrent.JSExecutionContext.Implicits.queue
的别名。此执行上下文在标准 JavaScript 事件循环中对作业进行排队。
请注意,任务是异步执行的,但不是并行的,因为 JavaScript 本身没有并行性的概念。如果需要并行性,则需要使用 Web Workers,但这些 Worker 不提供 Future
所需的共享内存模型。
适用于 Scala 的旧答案.js <0.6.0
scala.scalajs.concurrent.JSExecutionContext
中有 2 个现有和工作ExecutionContext
,在内部对象Implicits
中隐式版本。只需导入对您有意义的一个(可能是queue
,另一个实际上不是异步的)。
import scala.scalajs.concurrent.JSExecutionContext.Implicits.queue
// now you get to play with Futures
相关文章:
- 可以't让我的if语句处理js中的html表单输入
- 使用agility.js进行页面布局和合成
- 使用Clipboard.js复制span文本
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 强制模板刷新ember.js
- 如何编写HTML输入的JS内联
- Angular JS IE9 Hashbang url rewriting
- 使用JS将数组转换为json对象
- Node.js v6.2.0类扩展不是函数错误
- 当js函数's已执行
- 要求未定义JS回调参数
- 在自定义mean.io包中使用angular-chart.js作为依赖项
- 无法在数据endVal中设置值=“”;{{ucount}}”;使用Angular JS的CountUp
- 如何从Java/scala调用js美化程序
- 如何更改<svg>标记为<img>用js标记
- 如何使用 node.js 比较两个 json 数组
- 斯卡拉的未来.js
- 如何从datetimepicker js中禁用未来的日期和时间
- 不允许转换为未来年份的时刻.js
- Paper.js未来飞溅