Simple Async Example

35 views
Skip to first unread message

Tyler Pachal

unread,
Feb 8, 2016, 1:10:41 PM2/8/16
to play-framework
Hi everyone, 

I am trying to get a simple async example route going.  Coming from a background of NodeJS, I am having a bit of a hard time understanding using callbacks AND having a return value.

def asyncTest = Action {

    val willBeInt = Future {
        Thread.sleep(5000)
        100
    }

    willBeInt.onComplete({
        case Success(value) => Ok(s"Value = $value")
        case Failure(e) => Failure(e)
    })
}
This results in a compile-time error:

Overloaded method value [apply] cannot be applied to  (Unit)

If someone could point me in the right direction, that would be awesome!


Thanks


Will Sargent

unread,
Feb 8, 2016, 1:26:30 PM2/8/16
to play-fr...@googlegroups.com
Hi Tyler,

onComplete doesn't return anything.  You want to use Action.async and willBeInt.map { result => ... }

You should use willBeInt.recover { case e:Exception => InternalServerError() } if you want to do any exception handling on the future.

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/7e48318f-f494-4652-8f3b-088aa9f78076%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tyler Pachal

unread,
Feb 8, 2016, 1:59:52 PM2/8/16
to play-framework
Thanks!
Reply all
Reply to author
Forward
0 new messages