Last active
March 14, 2019 17:51
-
-
Save nachinius/bd361d465d726c2a350b5932a2138e76 to your computer and use it in GitHub Desktop.
Execute a future in a seq, and recovered result (not stack safe)
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
type A[E,T] = (E, Try[T]) | |
def seqOfFuturesRecovered[T, E](elems: Seq[E])(func: E => Future[T])(implicit executionContext: ExecutionContext): Future[Seq[A[E,T]]] = { | |
elems.foldLeft(Future(Seq.empty[A[E, T]])) { (acc, elem) => | |
acc.flatMap { prevSeq: Seq[(E, Try[T])] => | |
FutureOps(func(elem)).futureToTry.map( (elem, _)).map { prevSeq :+ _} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment