-
Notifications
You must be signed in to change notification settings - Fork 22
Coroutines: Wait for coroutine to finish
Devrath edited this page Dec 30, 2023
·
1 revision
- Here observe the output, In the parent co-routine, which is a view model scope, We have launched a new coroutine scope.
- But even though the inner co-routine is suspended since we have used
join()
, It will wait meaning theno-4
is executed after the inner scope.
fun waitForCoRoutineToFinish() {
//println("Before the view model scope")
viewModelScope.launch {
println("Before the coroutine scope") // - 1
CoroutineScope(EmptyCoroutineContext).launch {
println("Before the delay") // - 2
delay(1000)
println("After the delay") // - 3
}.join()
println("After the coroutine scope") // - 4
}
//println("After the view model scope")
}
Before the coroutine scope
Before the delay
After the delay
After the coroutine scope