<@U5EV2Q8H0> ``` class Service { private val ...
# coroutines
u
@zhuinden
Copy code
class Service {
    private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)

    suspend fun doSomething(): String = scoped x@{
        delay(2000)
        Log.d("Default", "returning something")
        return@x "Something"
    }

    suspend fun doSomething2(): String = scoped x@{
        delay(2000)
        Log.d("Default", "returning something")
        return@x "Something"
    }

    suspend fun doSomething3(): String = scoped x@{
        delay(2000)
        Log.d("Default", "returning something")
        return@x "Something"
    }

    private suspend fun <T> scoped(body: suspend CoroutineScope.() -> T): T {
        val deferred = scope.async(block = body)
        return deferred.await()
    }

    fun close() {
        scope.cancel()
    }
}