Skip to content

Instantly share code, notes, and snippets.

@kilink
Created February 11, 2017 07:26
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kilink/18333e6b262189df30b9d37713f61e31 to your computer and use it in GitHub Desktop.
Save kilink/18333e6b262189df30b9d37713f61e31 to your computer and use it in GitHub Desktop.
OkHttp Java 8 CompletableFuture extension method
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Response
import java.io.IOException
import java.util.concurrent.CompletableFuture
fun Call.executeAsync(): CompletableFuture<Response> {
val future = CompletableFuture<Response>()
enqueue(object : Callback {
override fun onResponse(call: Call, response: Response) {
future.complete(response)
}
override fun onFailure(call: Call, e: IOException) {
future.completeExceptionally(e)
}
})
return future
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment