add Future.filter for use in for comprehensions
This commit is contained in:
parent
cc1755f65b
commit
bbe2a3beb7
1 changed files with 6 additions and 0 deletions
|
|
@ -198,6 +198,12 @@ sealed trait Future[T] {
|
|||
f(optr.get)
|
||||
}
|
||||
|
||||
final def filter(p: T => Boolean): Future[T] = {
|
||||
val f = new DefaultCompletableFuture[T](timeoutInNanos, NANOS)
|
||||
onComplete (_.value.foreach(_.fold(f.completeWithException, r => f.complete(try { if (p(r)) Right(r) else Left(new MatchError(r)) } catch { case e => Left(e) }))))
|
||||
f
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current result, throws the exception is one has been raised, else returns None
|
||||
*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue