gpt4 book ai didi

asynchronous - 如何使用 Play 2.1.1 异步请求和 future

转载 作者:行者123 更新时间:2023-12-04 05:15:51 24 4
gpt4 key购买 nike

下面的代码无法编译,我不知道如何修复它。

def doAsync(n: Int) = Action {
import scala.concurrent.ExecutionContext.Implicits.global

Async {
val f1 = Future.successful(n)

f1.map(x => x match {
case 10 => Ok("first")
case _ => {
val f2 = Future.successful(n)
f2.map(y => Ok("second"))
}
})
}
}

线路 f2.map(y => Ok("second"))产生 编译错误 :

type mismatch; found: scala.concurrent.Future[play.api.mvc.SimpleResult[String]] required: play.api.mvc.Result



核心 "business logic" is: f2只有必须运行 if n is not 10 .

最佳答案

f1 将成为结果的 future 。

您将返回案例 10 的结果,否则返回结果的 future 。您需要将结果放入相同的术语中。尝试:

f1 flatMap {x => x match {
case 10 => Future.successful(Ok("first"))
case _ => {
val f2 = Future.successful(n)
f2.map(y => Ok("second"))
}
}

关于asynchronous - 如何使用 Play 2.1.1 异步请求和 future ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17266521/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com