hmole
06/26/2019, 5:09 AMCoroutineScope
receiver). Is there a better way? Sample in a thread.private suspend fun <T> Call<T>.executeAsync(): Response<T> {
val call = this
return suspendCancellableCoroutine { continuation ->
continuation.invokeOnCancellation { call.cancel() }
CoroutineScope(continuation.context).launch {
val result = runCatching(call::execute)
continuation.resumeWith(result)
}
}
}
streetsofboston
06/26/2019, 5:59 AMsuspend
function, you can call the function called coroutineScope
and call launch
from within its lambda.
However, since coroutineScope
waits/suspends until all it's children have finished (including this launch
) and you need a result, why not just use a more simple 'async { }' with 'await()' and listen to the completion/cancellation of the Job (Deferred) returned by this 'async' call? No suspendCancellableCoroutine
and no coroutine-scope.hmole
06/26/2019, 6:04 AMinvokeOnCompletion
is not synchronious(I know docs say it is, but it's not(at least in my case)) cancellation, so I can't use that.louiscad
06/26/2019, 10:27 AMstreetsofboston
06/26/2019, 11:13 AMcause
you can check whether the job just ended or ended with an exception or cancellation.
https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/invoke-on-completion.html