scala - Pattern for responding with async result -


i trying following example working:

def asynctest = action {      val willbeint = future {         thread.sleep(5000)         100     }      willbeint.oncomplete({         case success(value) => ok(s"value = $value")         case failure(e) => failure(e)     }) } 

but getting error overloading method:

overloaded method value [apply] cannot applied  (unit) 

i'm coming background in nodejs , struggling figure out how these callbacks supposed work while simultaneously returning result appease method signature.

think of action function returns promise, rather function accepts callback. in scala terminology, you'll returning future. play's internals calling oncomplete (or similar) on own (analagous javascript promise's then function).

specifically, compilation error due fact oncomplete returns unit, when action block expecting return future. can use map transform willbeint future play looking for:

def asyntest = action.async {   val willbeint = future {     thread.sleep(5000)     100   }    // note need   // import scala.concurrent.executioncontext.implicits.global   // able call `map` here   willbeint map { value =>     ok(s"value = $value")   } recover {     case e: throwable => internalservererror(e.tostring)   } } 

for reading, check out the docs future, , the docs action


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -