Add methods to Future for map, flatMap, and foreach
This commit is contained in:
parent
6ed7b4f442
commit
cc1755f65b
1 changed files with 18 additions and 0 deletions
|
|
@ -180,6 +180,24 @@ sealed trait Future[T] {
|
|||
}
|
||||
}
|
||||
|
||||
final def map[A](f: T => A): Future[A] = {
|
||||
val fa = new DefaultCompletableFuture[A](timeoutInNanos, NANOS)
|
||||
onComplete (_.value.foreach(_.fold(fa.completeWithException, r => fa.complete(try { Right(f(r)) } catch { case e => Left(e) }))))
|
||||
fa
|
||||
}
|
||||
|
||||
final def flatMap[A](f: T => Future[A]): Future[A] = {
|
||||
val fa = new DefaultCompletableFuture[A](timeoutInNanos, NANOS)
|
||||
onComplete (_.value.foreach(_.fold(fa.completeWithException, r => try { f(r).onComplete(fa.completeWith(_)) } catch { case e => fa.completeWithException(e) })))
|
||||
fa
|
||||
}
|
||||
|
||||
final def foreach(f: T => Unit): Unit = onComplete { ft =>
|
||||
val optr = ft.result
|
||||
if (optr.isDefined)
|
||||
f(optr.get)
|
||||
}
|
||||
|
||||
/**
|
||||
* 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