Created
August 5, 2016 08:58
-
-
Save kasia-kittel/25200f5da67257589bcc8bdc03a1c6fa to your computer and use it in GitHub Desktop.
Merging many Future[Seq] into one Future[Seq]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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