R
R
Rail Khusnutdinov2015-10-29 23:57:42
Scala
Rail Khusnutdinov, 2015-10-29 23:57:42

How to convert Future[String] type to String in Scala?

Good evening! I am new to Scala programming. Can you please explain how to convert the Future[any type] type to a non-Future type, or to pass the already computed value to another variable with a non-Future type?
I apologize in advance if the question is not correct.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Kondaurov, 2015-10-30
@rail-ka

The only way to get rid of the Future and work directly with the result is to wait for that result to be evaluated. Use concurrent.Await(f: Future[Any], duration) for this.
val f = concurrent.Future.successful("myFutureString")
val notFuture = concurrent.Await(f, 1.second)
print(notFuture)
Using Await is not recommended, it should be done in extreme cases. I use Await when I work with external Api services, so as not to ask requests, but inside my application I use map.
The above example can also be done like this (recommended):
val f = concurrent.Future.successful("myFutureString")
f.map(res => print(res))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question