这个线程让我知道如何构建我的代码: Scala-way to handle conditions in for-comprehensions?
有问题的部分:
// First create the JSON
val resultFuture: Future[Either[Failure, JsResult]] = for {
userRes <- userDao.findUser(userId)
user <- userRes.withFailure(UserNotFound).right
authRes <- userDao.authenticate(user)
auth <- authRes.withFailure(NotAuthenticated).right
goodRes <- goodDao.findGood(goodId)
good <- goodRes.withFailure(GoodNotFound).right
checkedGood <- checkGood(user, good).right
} yield renderJson(Map("success" -> true)))
这是我不明白的行:
user <- userRes.withFailure(UserNotFound).right
authRes <- userDao.authenticate(user)
userRes.withFailure (UserNotFound).right映射到userDao.authenticate(user)。这将创建一个新的 Either,其右侧有一个 Future,对吗?
怎么能
val resultFuture: Future[Either[Failure, JsResult]]
属于它的类型。我认为应该有另一个未来,而不是 JsResult。谁能给我解释一下?
编辑:由于 cmbaxter 和 Arne Claassen 证实了这一点,新的问题是:我应该如何编写这段代码,这样它看起来并不难看,而是干净和结构化?