https://kotlinlang.org logo
#coroutines
Title
# coroutines
k

kirillrakhman

04/26/2017, 7:53 AM
@gildor you've done the retrofit couroutine library. would you consider making one for okhttp?
g

gildor

04/26/2017, 8:01 AM
yeah, I want to start work. But to be honest I have not so many practical use cases where coroutines would be useful for Okhttp
k

kirillrakhman

04/26/2017, 8:02 AM
I thnik it's only
Call.await
. It's virtually identical to retrofit's
Call
g

gildor

04/26/2017, 8:06 AM
Okay, maybe I’ll publish something on this weekend. If you have some thoughts about coroutines for okhttp, be free to message me
👍 1
k

kirillrakhman

04/26/2017, 8:07 AM
👍
u

uhe

04/26/2017, 1:05 PM
I've used this in our project:
Copy code
suspend fun Call.await(): Response = suspendCancellableCoroutine { cont ->
    enqueue(object : Callback {
        override fun onResponse(call: Call, response: Response) {
            cont.resume(response)
        }

        override fun onFailure(call: Call, e: IOException) {
            if (e is InterruptedIOException || cont.isCancelled) return
            cont.resumeWithException(e)
        }
    })
    cont.invokeOnCompletion { if (cont.isCancelled) cancel() }
}
3 Views