My trait method is:
userService{
def link(current: U, to:User): Future[U]
def findUserByEmail(email:String):Future[Option[User]]
}
when I execute I use:
for(link(currentUser, userService.findUserByEmail(email).result(Duration(1000, MILLISECONDS)).get)){
...
}
and the error is:
[error] G:\testprojects\mifun\modules\app\controllers\
ProviderController.scala:130: Don't call `Awaitable` methods directly, use the `
Await` object.
I do not know why here must use await object instead of awaitable methods, and how to change it correctly.
If you want to block you need to use Await.result(userService.findUserByEmail(email), 1000 millis), note that blocking is in general a bad idea as it blocks your main thread waiting for the specified result to return, take a look at onComplete for example.
Something like this:
val futureLink = findUserByEmail(user) flatMap {
maybeUser => maybeUser map (user => link(currentUser, user))
}
futureLink onComplete {
case Success(user:User) => ...
case Success(None) => ...
case Failure(ex) => ...
}
or, if you really need to block, you can do Await.result on futureLink.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With