I have a tail recursive implementation as below
@tailrec
def generate() : String = {
val token = UUID.randomUUID().toString
val isTokenExist = Await.result(get(token), 5.seconds).isDefined
if(isTokenExist) generate()
else token
}
get(token)
will return a Future[Option[Token]]
.
I know that blocking in a Future is not good. I tried to return a Future[String]
instead of String
. But seems that its not possible unless I wait for isTokenExist
to be completed.
Any other way/suggestion to implement this?