title: Nested Callback function with coroutines
body: <p>I want to get a response from callback function async/await style of javascript using kotlin coroutines.</p>
  <p>Here is my callback functions</p>
  <pre><code>offlineCatalog.findOfflineVideoById(id, object : OfflineCallback&lt;Video&gt; {
       override fun onSuccess(video: Video?) {
           video?.let { 
                //Return This Video
           } ?: kotlin.run { 
                findVideoOnline(id, state) 
           }
      }
      override fun onFailure(throwable: Throwable?) {
           findVideoOnline(id, state)
      }
  })
  
  onlineCatalog.findVideoByID(id, object : VideoListener() {
      override fun onVideo(video: Video?) {
           video?.let { 
               //Return This Video 
           } ?: kotlin.run { 
              Log.e(&quot;Return Error&quot;) 
           }
      }
      override fun onError(errors: MutableList&lt;CatalogError&gt;) {
           super.onError(errors)
           Log.e(&quot;Return Error&quot;)
      }
  })
  </code></pre>
  <p>I want to call function that will return video object from <code>OfflineCatalog</code> if error in <code>OfflineCatalog</code> then search from <code>OnlineCatalog</code>.</p>
  <p>such as</p>
  <pre><code> try{
       val video:Video? = getVideo(id:String)
       //do something 
   }catch(throwable:Throwable){
       Log.e(&quot;Video not found&quot;)
   }
  </code></pre>
  <p><strong>Update: My Implementatio</strong>n</p>
  <p>this is what I came up with</p>
  <pre><code>suspend fun getVideo(id: String): Video? = withContext(Dispatchers.IO) {
      var video = getVideoOffline(id)
      video?.let { video } ?: kotlin.run { getVideoOnline(id) }
  }
  
  suspend fun getVideoOffline(id: String): Video? = suspendCancellableCoroutine { cont -&gt;
      (offlineCatalog.findOfflineVideoById(id, object : OfflineCallback&lt;Video&gt; {
          override fun onSuccess(video: Video?) = cont.resume(video)
          override fun onFailure(throwable: Throwable?) = cont.resume(null)
      }))
  }
  
  suspend fun getVideoOnline(id: String): Video? = suspendCancellableCoroutine { cont -&gt;
      catalog.findVideoByID(id, object : VideoListener() {
          override fun onVideo(video: Video?) = cont.resume(video)
          override fun onError(errors: MutableList&lt;CatalogError&gt;) = cont.resume(null)
      })
  }
  </code></pre>
  <p>Usage-</p>
  <pre><code>CoroutineScope(Dispatchers.Main).launch {
      getVideo(id)?.let { 
          //Do Something
      } ?: kotlin.run{
          //Video Not Found
      }
  }
  </code></pre>