When should you use Future, Future.successful and Future.failed
In this blog post, I would recommend when should you use Future()/Future.successful/Future.failed?
- Use Future.apply() or simply Future() (i.e., Future block): In the situations, where something to be done asynchronously that can complete sometime in future and may deal with some time-consuming operations such as network calls, database operations communicate with one or many other services, processing huge data by consuming multiple cores and etc.
- Use Future.successful: When a literal or already computed value to be passed back as a successful future response.
- Use Future.failed: When a known and literal exception to be thrown back without performing any further actions in the future.
- Future.fromTry: When you already computed Try a value
Future.successful, Future.failed, and Future.fromTry when you need to create an instance of the Future and you already have the value.
Reference:
https://viktorklang.com/blog/Futures-in-Scala-protips-3.html
Thanks.