Knows Not Much
Knows Not Much

Reputation: 31546

Equivalent of flatten method in Scala 2.11 to handle nested futures

In scala 2.12 I can write the following code

import scala.concurrent._
import scala.concurrent.ExecutionContext.Implicits.global
val x = Future(Future(10))
val y = x.flatten

However, scala 2.11 does not provide a flatten method. Any idea how can I achieve the same result in scala 2.11

edit: Can the cats library help?

Upvotes: 3

Views: 316

Answers (2)

Evgeny Veretennikov
Evgeny Veretennikov

Reputation: 4239

Don't know about Cats, but Scalaz has method join for that:

val y = x.join

Moreover, this method works for all Monads.

Upvotes: 1

nmat
nmat

Reputation: 7591

Use flatMap:

val y = x.flatMap(identity)

Upvotes: 7

Related Questions