Skip to content

Instantly share code, notes, and snippets.

@nachinius
Last active March 14, 2019 17:51
Show Gist options
  • Save nachinius/bd361d465d726c2a350b5932a2138e76 to your computer and use it in GitHub Desktop.
Save nachinius/bd361d465d726c2a350b5932a2138e76 to your computer and use it in GitHub Desktop.
Execute a future in a seq, and recovered result (not stack safe)
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