5
votes

Is there an equivalent to the mapAsync() method, but for filter?

Here is an example using pseudo code:

val filter: T => Future[Boolean] = /.../

source.filter(filter).runWith(/.../)
       ^^^^^^

Thanks

1

1 Answers

9
votes

I don't think there is a direct method of Flow or Source that has the capability you're looking for, but a combination of the available methods will get you what you want:

def asyncFilter[T](filter: T => Future[Boolean], parallelism : Int = 1)
                  (implicit ec : ExecutionContext) : Flow[T, T, _] =
  Flow[T].mapAsync(parallelism)(t => filter(t).map(_ -> t))
         .filter(_._1)
         .map(_._2)