2018-01-03a few seconds read (About 83 words)Learning from Open Source Making Deferred in KotlinIssue #135 From https://github.com/JakeWharton/retrofit2-kotlin-coroutines-adapter/blob/master/src/main/java/com/jakewharton/retrofit2/adapter/kotlin/coroutines/experimental/CoroutineCallAdapterFactory.kt#L86 12345678910111213141516171819202122232425override fun adapt(call: Call<T>): Deferred<T> { val deferred = CompletableDeferred<T>() deferred.invokeOnCompletion { if (deferred.isCancelled) { call.cancel() } } call.enqueue(object : Callback<T> { override fun onFailure(call: Call<T>, t: Throwable) { deferred.completeExceptionally(t) } override fun onResponse(call: Call<T>, response: Response<T>) { if (response.isSuccessful) { deferred.complete(response.body()!!) } else { deferred.completeExceptionally(HttpException(response)) } } }) return deferred}#kotlin