0

I am needing explaining on how future is working in my play controller.

I have some code in my service layer which is calling a Java library API. When I first call my Java API like this in a controller:

def someController() = Action {
  someJavaAPI()
  Ok("done")
}

For this controller, I did not see any action. I expect action because of a call to someJavaAPI. Then somebody told me that someJavaAPI is dying because the controller is finishing too early. Is that right?

So I changed the code to be like this: I am wrapping someJavaAPI() method in a Future

def someJavaAPI() = Future {....code....}

Now I calling this like this in my contraoller:

def someController() = Action {
  someJavaAPI().map{ _ =>  Ok("done") }
}

This is working very good.

My question is: is wrapping someJavaAPI in future makes controller thread wait until it finished and then does map function?

Also, what is Promise? Can I use Promise here? I am not sure.

Krzysztof Atłasik
  • 21,985
  • 6
  • 54
  • 76
Mojo
  • 1,152
  • 1
  • 8
  • 16
  • Please refer https://docs.scala-lang.org/overviews/core/futures.html and https://stackoverflow.com/questions/18960339/clarification-needed-about-futures-and-promises-in-scala. It will answer most of your questions – Chaitanya Mar 26 '19 at 16:15
  • 1
    How exactly did you call java API in both cases? – Andriy Kuba Mar 26 '19 at 21:09
  • @AndriyKuba I am not sure what you are meaning. Java library method is called like I show, – Mojo Mar 27 '19 at 09:09

0 Answers0