Callbacks

So, now that we have the ability to resume at a give point by using the label, we need to modify the other suspending functions that are called from getUserSummary(), so that they also receive CoroutineImpl. Let's say that we, as the compiler, have modified the fetchProfile() and validateTerms() functions so that they received a Continuation<Any?>—just like we did for getUserSummary()—and called doResume() upon the completion of their execution. Then we could invoke them like this:

when(state.label) {
0 -> { // Label 0 -> first execution
logger.log("fetching summary of $id")
fetchProfile(id, sm)
return
}
1 -> { // label 1 -> resuming
calculateAge(profile.dateOfBirth)
validateTerms(profile.country, age, sm)
return
}
2 -> // label 2 -> resuming and terminating
UserSummary(profile, age, terms)
}

By having both fetchProfile() and validateTerms() call the continuation that they receive whenever they finish their execution, we are having them call the continuation that we implemented in getUserSummary(), thus resuming its execution.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.35.72