title: Existing 3-function callback to Kotlin Coroutines
body: <p>I have a general question with a specific example:  I'd like to use Kotlin coroutine magic instead of callback hell in Android when taking a picture.</p>
  
  <pre><code>manager.openCamera(cameraId, object : CameraDevice.StateCallback() {
      override fun onOpened(openedCameraDevice: CameraDevice) {
          println("Camera onOpened")
          // even more callbacks with openedCameraDevice.createCaptureRequest()....
      }
  
      override fun onDisconnected(cameraDevice: CameraDevice) {
          println("Camera onDisconnected")
          cameraDevice.close()
      }
      ...
  </code></pre>
  
  <p>How would I convert that to something less ugly?  <strong><em>Is it possible to take an average callback with three or so functions, and turn it into a promise-chain by designating the primary flow as the promise-result path?</em></strong> And if so, should/do I use coroutines to make it async?</p>
  
  <p>I'd love something with async and .await that would result in</p>
  
  <pre><code>manager.open(cameraId).await().createCaptureRequest()
  </code></pre>
  
  <p>I'm trying to do it through something like the following, but I don't think I'm using <code>CompletableDeferred</code> right!</p>
  
  <pre><code>suspend fun CameraManager.open(cameraId:String): CameraDevice {
      val response = CompletableDeferred&lt;CameraDevice&gt;()
      this.openCamera(cameraId, object : CameraDevice.StateCallback() {
          override fun onOpened(cameraDevice: CameraDevice) {
              println("camera onOpened $cameraDevice")
              response.complete(cameraDevice)
          }
  
          override fun onDisconnected(cameraDevice: CameraDevice) {
              response.completeExceptionally(Exception("Camera onDisconnected $cameraDevice"))
              cameraDevice.close()
          }
  
          override fun onError(cameraDevice: CameraDevice, error: Int) {
              response.completeExceptionally(Exception("Camera onError $cameraDevice $error"))
              cameraDevice.close()
          }
      }, Handler())
      return response.await()
  }
  </code></pre>