Java/Scala Future/Promise Map *headsplode*

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

# Java/Scala Future/Promise Map

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&lt;ObjectNode&gt; getEmployees(final Optional&lt;String&gt; filterEmail)
    {
        // Call the webservice
        Promise&lt;Response&gt; promiseOfEmployees = this.xero.getEmployees(filterEmail);

        // Map into an ObjectNode
        Promise&lt;ObjectNode&gt; promiseJson = promiseOfEmployees.map(new F.Function&lt;WS.Response, ObjectNode&gt;()
        {
            @Override
            public ObjectNode apply(final Response response) throws Throwable
            {
                JsonNode responseJson = response.asJson();

                ObjectNode json = Json.newObject();

                json.put(&quot;foo&quot;, &quot;bar&quot;);

                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 =&gt;
      val jsonResponse: JsonNode = response.asJson();
      val json: ObjectNode = Json.newObject()
      json.put(&quot;foo&quot;, &quot;bar&quot;);
      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 =&gt;
      val jsonResponse = response.asJson();
      val json = Json.newObject()
      json.put(&quot;foo&quot;, &quot;bar&quot;);
      json // Return
    }

    // Return
    return Promise.wrap(promiseJson)
  }

<3 Glenn / devalias