Suppose I have several futures and need to wait until either any of them fails or all of them succeed.
For example: Let there are 3 futures: f1
, f2
, f3
.
If
f1
succeeds andf2
fails I do not wait forf3
(and return failure to the client).If
f2
fails whilef1
andf3
are still running I do not wait for them (and return failure)If
f1
succeeds and thenf2
succeeds I continue waiting forf3
.
How would you implement it?
Here is a solution without using actors.
This question has been answered but I am posting my value class solution (value classes were added in 2.10) since there isn't one here. Please feel free to criticize.
ConcurrentFuture is a no overhead Future wrapper that changes the default Future map/flatMap from do-this-then-that to combine-all-and-fail-if-any-fail. Usage:
In the example above, f1,f2 and f3 will run concurrently and if any fail in any order the future of the tuple will fail immediately.