Combining futures

Say I have:

val a: Future[A]
val b: Future[B]

And I want to combine them into:

val y: Future[(Try[A], Try[B])]

How can I do that?

There is a method zip on Future, but that gives Future[(A, B)], i.e. it fails if either a or b fails, thus potentially losing information.

You may use transform to lift the hidden error channel of the Future into an explicit one, and then zip them like this:

val y = a.transform(t => Success(t)) zip b.transform(t => Success(t))
3 Likes

Thanks. It’s odd that this is not in the stdlib though. Same with Future.sequence.