Skip to content

Instantly share code, notes, and snippets.

@kasia-kittel
Created August 5, 2016 08:58
Show Gist options
  • Save kasia-kittel/25200f5da67257589bcc8bdc03a1c6fa to your computer and use it in GitHub Desktop.
Save kasia-kittel/25200f5da67257589bcc8bdc03a1c6fa to your computer and use it in GitHub Desktop.
Merging many Future[Seq] into one Future[Seq]
import scala.concurrent.ExecutionContext.Implicits.global
case class Error(name:String)
type Errors = Seq[Error]
trait getErrors {
def errors(): Future[Errors]
}
class Validation
object TypeValidation extends Validation with getErrors {
def errors() =
Future(Seq(Error("o1e1"), Error("o1e2"), Error("o1e3")))
}
object LengthValidation extends Validation with getErrors {
def errors() =
Future(Seq(Error("o2e1"), Error("o2e2")))
}
val validations = Seq(TypeValidation, LengthValidation)
val errors: Future[Errors] = Future.sequence(
validations.map(_.errors())
).map(_.flatten)
errors.value.get
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment