A side by side comparison (now that I finally got it figured out!) of Play Framework v2.2.x promise unwrapping in Java and Scala. Hopefully this will be able to save some of you a bunch of mind numbing ponderings and failures.
<3 /dev/alias
https://gist.github.com/0xdevalias/1d3b44312d8a68ddbaa5
A side by side comparison (now that I finally got it figured out!) of Play Framework v2.2.x promise unwrapping in Java and Scala.
Java
public Promise<ObjectNode> getEmployees(final Optional<String> filterEmail)
{
// Call the webservice
Promise<Response> promiseOfEmployees = this.xero.getEmployees(filterEmail);
// Map into an ObjectNode
Promise<ObjectNode> promiseJson = promiseOfEmployees.map(new F.Function<WS.Response, ObjectNode>()
{
@Override
public ObjectNode apply(final Response response) throws Throwable
{
JsonNode responseJson = response.asJson();
ObjectNode json = Json.newObject();
json.put("foo", "bar");
return json;
}
});
// Return
return promiseJson;
}
Scala
def getEmployees(filterEmail: Optional[String]): Promise[ObjectNode] = {
// Call the webservice
val promiseOfEmployees: Promise[Response] = this.xero.getEmployees(filterEmail)
// Map into an ObjectNode
val promiseJson: Future[ObjectNode] = promiseOfEmployees.wrapped().map { response =>
val jsonResponse: JsonNode = response.asJson();
val json: ObjectNode = Json.newObject()
json.put("foo", "bar");
json // Return
}
// Return
return Promise.wrap(promiseJson)
}
Scala (Implicit)
def getEmployees(filterEmail: Optional[String]): Promise[ObjectNode] = {
// Call the webservice
val promiseOfEmployees = this.xero.getEmployees(filterEmail)
// Map into an ObjectNode
val promiseJson = promiseOfEmployees.wrapped().map { response =>
val jsonResponse = response.asJson();
val json = Json.newObject()
json.put("foo", "bar");
json // Return
}
// Return
return Promise.wrap(promiseJson)
}
<3 Glenn / devalias