aepetelin aepetelin - 2 months ago 10
Ajax Question

Ajax response wait for future (scala)

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)
}

Answer

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)
  }
}
Comments