July 2018
Intermediate to advanced
266 pages
7h 11m
English
Now, our state machine is able to do almost everything it needs to do. The big missing part is to actually return the result of the operation somehow. Because this uses CPS, it isn't actually going to return the value in the classical sense; instead, it will use the first continuation it received as a callback, where it will send the result. This would be a complete implementation—minus error handling:
suspend fun getUserSummary(id: Int, cont: Continuation<Any?>) { val sm = cont as? GetUserSummarySM ?: GetUserSummarySM() when(sm.label) { 0 -> { // Label 0 -> first execution sm.cont = cont logger.log("fetching summary of $id") sm.label = 1 fetchProfile(id, sm) return } 1 -> { // label 1 -> ...
Read now
Unlock full access