0

I am new to Scala and Future. In a scenario like following

val a = Future(10)
println(a)
val b = Future{20}
println(b)

The output is

 Future(Success(10))
 Future(<not completed>)

I did not understand why using curly braces changes the output. Or What is the basic difference between these two Futures.

2
  • 7
    Small hint: if you want to put value that already calculated into Future it is better to use Future.successful(value). This will not schedule extra task in thread pool and Future value will be available instantly. Commented Aug 27, 2021 at 7:55
  • Have a look here docs.scala-lang.org/style/control-structures.html
    – mfirry
    Commented Aug 27, 2021 at 8:28

1 Answer 1

9

In this case there is no difference. The problem is that you are printing the Future immediately after creating it so it is up to the scheduler (and random factors) whether or not the Future has completed or not.

I just ran this and got

Future(<not completed>)
Future(<not completed>)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.