1

if I have a Future[A] as a result (the last line) in router's post("/some") path, the Ajax client can't get the response and exceeds a timeout. Await doesn't work. Future onComplete/onSuccess {...} works ok, but for the server, so how to translate it to the client as a response? (Scalatra framework)

server:

post("/stations/test") {
  Future[Int] {
    // parse jsonData ...
    Thread.sleep(3000)
    1
  }.onComplete { x =>
    // do something on server ...
  }
}

client:

@JSExport
def testFuture() = {
  val request = Ajax.post("/stations/test", jsonData)
}
Sign
  • 1,919
  • 18
  • 33
aepetelin
  • 13
  • 3

1 Answers1

0

onComplete returns Unit. But, you need proper response to be sent from the server to the client. Use map on the future to create the response.

post("/stations/test") {
  Future[Int] {
    // parse jsonData ...
    Thread.sleep(3000)
    1
  }.map { data =>
    Response(data)
  }
}
Nagarjuna Pamu
  • 14,737
  • 3
  • 22
  • 40
  • 1
    thanks! If I put response (type of HttpServletResponse) instead of "Response(data)" it works well. Have a good weekend! and client side should be like: Ajax.post("/stations/test", "abc") .recover { case dom.ext.AjaxException(req) => //... } .map(req => { // handle all status codes }) – aepetelin Sep 18 '16 at 00:47