I'm learning futures, and I'm trying to create a method that, take two futures as parameter
(f
and g
) and return the first future that was successfully completed, otherwise it returns f
or g
.
Some use cases to illustrate the behaviour of my method are :
Future 1 | Future 2 | Result
Success First Success Second Future 1
Success First Failure Second Future 1
Success Second Success First Future 2
Success Second Failure First Future 1
Failure First Failure Second Future 2 (because we had a failure on Future 1, so try to see what is the result Future 2)
So I created this method :
def successRace(f: Future[T], g: Future[T]): Future[T] = {
val p1 = Promise[T]()
val p2 = Promise[T]()
val p3 = Promise[T]()
p1.completeWith(f)
p2.completeWith(g)
p3. ????
p3.future
}
And now, how can I know which one completed first ?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…