如何在 scala 中展平未来 - “val _2flat: Future[Option[Future[List[Long]]]]

How to flatten a Future in scala - "val _2flat: Future[Option[Future[List[Long]]]]

我有这个数据:

val _2BeFlat: Future[Option[Future[List[Long]]]] = ...

我需要:

val flat: Future[Option[List[Long]]] = ...

有办法吗?

这应该可以解决问题:

val _2BeFlat: Future[Option[Future[List[Long]]]] = _

val flat: Future[Option[List[Long]]] = _2BeFlat.flatMap {
  _ match {
    case None => Future.successful(None)
    case Some(future) => future.map(Some(_))
  }
}