윤동환
07/17/2023, 1:23 PMsuspend fun main() {
route()
}
suspend fun route() = coroutineScope {
logger.warn { "start rout! scope: $this, context: $coroutineContext" }
service()
logger.warn { "routing finished! scope: $this, context: $coroutineContext" }
}
suspend fun service() = coroutineScope {
logger.warn { "service executed! scope: $this, context: $coroutineContext" }
sendMessage("inner")
logger.warn { "service finished! scope: $this, context: $coroutineContext" }
}
fun CoroutineScope.sendMessage(string: String) = launch {
delay(3000)
logger.warn { "sended message : $string, scope: $this, context: $coroutineContext" }
}
private val logger = KotlinLogging.logger {}
What I wanted for the code is route function is ends before service function that launch coroutines that sending message.
But the code wasn’t.
So how can i finish rout function not waiting until the service function finished?denis090712
07/17/2023, 1:36 PMsuspend fun CoroutineScope.service() = launch {
logger.warn { "service executed! scope: $this, context: $coroutineContext" }
sendMessage("inner")
logger.warn { "service finished! scope: $this, context: $coroutineContext" }
}
denis090712
07/17/2023, 1:38 PMcoroutineScope {}
builder waits until all children coroutines are finished윤동환
07/17/2023, 1:52 PMdenis090712
07/17/2023, 1:54 PMlaunch
or async
builders to do any work “in background”Riccardo Lippolis
07/17/2023, 5:02 PM