title: Modify the Deferred result
body: <p>Given an API (implemented by <a href="http://square.github.io/retrofit/" rel="noreferrer">Retrofit</a>) that returns a model. I wrap an old fashioned <a href="https://square.github.io/retrofit/2.x/retrofit/retrofit2/Call.html" rel="noreferrer">Call</a> into a <code>Deferred</code> using the extention function:</p>
  
  <pre><code>fun &lt;T&gt; Call&lt;T&gt;.toDeferred(): Deferred&lt;T&gt; {
      val deferred = CompletableDeferred&lt;T&gt;()
  
      // cancel request as well
      deferred.invokeOnCompletion {
          if (deferred.isCancelled) {
              cancel()
          }
      }
  
      enqueue(object : Callback&lt;T&gt; {
          override fun onFailure(call: Call&lt;T&gt;?, t: Throwable) {
              deferred.completeExceptionally(t)
          }
  
          override fun onResponse(call: Call&lt;T&gt;?, response: Response&lt;T&gt;) {
              if (response.isSuccessful) {
                  deferred.complete(response.body()!!)
              } else {
                  deferred.completeExceptionally(HttpException(response))
              }
          }
      })
  
      return deferred
  }
  </code></pre>
  
  <p>Now I can get my model like this:</p>
  
  <pre><code>data class Dummy(val name: String, val age: Int)
  
  fun getDummy(): Deferred&lt;Dummy&gt; = api.getDummy().toDeferred()
  </code></pre>
  
  <p>But how can I <em>modify</em> the object inside the <code>Deferred</code> and return a <code>Deferred</code>:</p>
  
  <pre><code>fun getDummyAge(): Deferred&lt;Int&gt; {
      // return getDummy().age
  }
  </code></pre>
  
  <p>I'm new to coroutines so may be this is not how the things get done here. Assuming I'm an <a href="https://github.com/ReactiveX/RxJava" rel="noreferrer">RxJava</a> fan I'd implement this case like:</p>
  
  <pre><code>fun getDummy(): Single&lt;Dummy&gt; = api.getDummy().toSingle()
  
  fun getDummyAge(): Single&lt;Int&gt; = getDummy().map { it.age }
  </code></pre>
  
  <p>So should I try to return a <code>Deferred</code> from the <code>getDummyAge</code> function? Or may be it's better to declare a <code>suspended fun</code> whenever possible and call <code>deferred.await()</code> on all my api's methods?</p>