But, currently, we aren't actually incrementing the label, so the function will loop on label zero. That should be done right before the call to the other suspending functions:
when(state.label) {
0 -> { // Label 0 -> first execution
logger.log("fetching summary of $id")
sm.label = 1
fetchProfile(id, sm)
return
}
1 -> { // label 1 -> resuming
calculateAge(profile.dateOfBirth)
sm.label = 2
validateTerms(profile.country, age, sm)
return
}
2 -> // label 2 -> resuming and terminating
UserSummary(profile, age, terms)
}
Notice that, by default, the label is zero in CoroutineImpl. Also, notice that we set the value for the next piece of code, not for the current one.