wrover
oleksandr
private fun launchWhenDeferredResolves(block: suspend () -> Unit) { launch { try { someDeferred.await() } catch (t: Throwable) { if (t !is CancellationException) { closeConnection() // <= smth like this? } return@launch } block(someDeferred) } }
finally
fun main() = runBlocking { val job = launch(<http://Dispatchers.IO|Dispatchers.IO>) { val socket = ServerSocket(23546) socket.useCancellably { it.accept() } } delay(1000) job.cancel() } suspend inline fun <T : Closeable?, R> T.useCancellably(crossinline block: (T) -> R): R = suspendCancellableCoroutine { continuation -> continuation.invokeOnCancellation { this?.close() } continuation.resume(use(block)) }
A modern programming language that makes developers happier.