BZapper
BZapper

Reputation: 330

Flattening List + Future for Scala

I'm trying to await on a funky structure like this: Future[List[Future[List[Object]]]]. Await only gets rid of the outer most Future, is there a clean way of trimming this down to a simple List[Object]?

Upvotes: 2

Views: 991

Answers (1)

Jean Logeart
Jean Logeart

Reputation: 53819

Using Future.sequence:

val original: Future[List[Future[List[Object]]]] = // ...

val futureList = 
  original.flatMap(l => Future.sequence(l)) // Future[List[List[Object]]]
    .map(_.flatten)                         // Future[List[Object]]

Upvotes: 9

Related Questions