ursus
07/16/2019, 7:47 PMclass 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()
}
}