Could You please help me write it in a better way:
Future {
Thread sleep 200
5
} onComplete{
case Success(e) => Future {
doSomething(e)
Thread sleep 200
6
} onComplete {
case Success(e) =>
Future {
doSomething(e)
Thread sleep 200
} onComplete {
case Success(_) => println("finished")
case Failure(e) => e.printStackTrace()
}
case Failure(e) => e.printStackTrace()
}
case Failure(e) => e.printStackTrace()
}
Now the code looks bad, and if I added more futures this way it would become even worse... This is obviously an example to show the problem, so I would appreciate mentioning wider context.
@update If it is not clear enough, I will try to clarify. I have three futures, and want to execute the first, when it finishes execute the second, when it finishes - the third. The second future uses result of the first, and the third uses result of the second one. If one of futures fail, stack trace is printed and call sequence breaks. This is what I wanted to show in the code above, and I want to achieve it in better, non-nesting way, if possible.
@update 2 It would also be great if I would handle failure of each future separately