Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scala future error for " Don't call `Awaitable` methods directly, use the `Await` object."

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.

like image 276
user504909 Avatar asked Aug 05 '14 06:08

user504909


2 Answers

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.

like image 136
Ende Neu Avatar answered Nov 16 '22 15:11

Ende Neu


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.

like image 37
Ashalynd Avatar answered Nov 16 '22 13:11

Ashalynd