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?

    1. 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.
    2. Use Future.successful: When a literal or already computed value to be passed back as a successful future response.
    3. Use Future.failed: When a known and literal exception to be thrown back without performing any further actions in the future.
    4. 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

    https://functional.works-hub.com/learn/scala-future-blocks-and-futhers-methods-what-to-use-when-4acbd

     

    Thanks.