我有以下Scala代碼,使用Scala 2.10爲什麼Scala Future會使用flatMap返回None並理解?
package reactive
import scala.concurrent.future
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
object futuresum extends App {
def f1 = future { 12 }
def f2 = future { 13 }
val result1 = f1.flatMap(x => f2.map(y => x + y))
println(result1.value)
val result2 = for (x <- f1; y <- f2) yield (x + y)
println(result2.value)
}
該代碼使用應打印25
,但它打印None
:
None
None
什麼是錯的代碼?
編輯
現在,這對我的作品:
package reactive
import scala.concurrent.future
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.concurrent.Await
import scala.concurrent.duration.Duration
object futuresum extends App {
def f1 = future { 12 }
def f2 = future { 13 }
val result1 = f1.flatMap(x => f2.map(y => x + y))
result1.onComplete(b => println(b))
val result2 = for (x <- f1; y <- f2) yield (x + y)
result2.onComplete(b => println(b))
Await.result(result1, Duration.Inf)
Await.result(result2, Duration.Inf)
}