如何在 Scala 中组合 2 个 Futures

How to combine 2 Futures in Scala

我正在编写一个 CRUD rest api,在服务层合并 2 个 futures 时遇到了问题。

思路是将Entity插入到db中,然后通过id检索所有db生成的值。

我尝试了 Java 中的 andThen(),但它不能 return Future[Entity],它说它仍然是 Future[Long]

class Service {
  def find(id: Long): Future[Option[Entry]] = db.run(repo.findEntry(id))

  //TODO Fails: Expression of type Future[Long] doesn't conform to expected type Future[Option[Entity]]
  def insert(): Future[Option[Entry]] = db.run(repo.insertEntry())
            .andThen { case Success(id) =>
                find(id)
            }
}

class Repository {
  def findEntry(id: Long): DBIO[Option[Entry]] =
    table.filter(_.id === id).result.headOption

  def insertEntry()(implicit ec: ExecutionContext): DBIO[Long] =
    table returning table.map(_.id) += Entry()
}

我觉得答案很简单,就是找不到。

andThen 是为了副作用,它仍然是 returns 原始结果(第一个 Future 的)。

你想要flatMap.

 db.run(repo.insertEntry())
   .flatMap( id => find(id) )

flatMap 还带有一种特殊的语法,大多数人发现这种语法更具可读性(在他们习惯之后),尤其是如果有更多的步骤:

 for {
   id <- db.run(repo.insertEntry())
   entry <- find(id)
 } yield entry