[英]Scala futures and Await.result()
I expect the following code to result in syncResult
containing "string two", but instead, I get the error java.lang.NoClassDefFoundError: Could not initialize class
.我希望以下代码会导致包含“字符串二”的
syncResult
,但相反,我收到错误java.lang.NoClassDefFoundError: Could not initialize class
。
import scala.concurrent.{Await, Future}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
def randomFunction1(): Future[String] = {
Future.successful("string one")
}
def randomFunction2(): Future[String] = {
Future.successful("string two")
}
val asyncResult: Future[String] = for {
r1 <- randomFunction1()
r2 <- randomFunction2()
} yield r2
val syncResult: String = Await.result(
asyncResult,
1.second
)
I get similar results with the following.我得到了类似的结果。
import scala.concurrent.{Await, Future}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
def randomFunction1(): Future[String] = {
Future.successful("string one")
}
def randomFunction2(): Future[String] = {
Future.successful("string two")
}
val asyncResult: Future[String] = randomFunction1().flatMap(
r1 => {
randomFunction2()
}
)
val syncResult: String = Await.result(
asyncResult,
1.second
)
I'm using the Scala 2.12.2 interpreter to run this using :paste
.我正在使用 Scala 2.12.2 解释器使用
:paste
运行它。
What's wrong with my code?我的代码有什么问题?
这似乎是 Scala REPL 的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.