Dontsu
09/19/2023, 1:35 AMKotlin Coroutines deep diveMarcin Moskalaunderstanding Kotlin CoroutineHow does suspension work?suspend fun main() {
  println("Before")
  suspendCoroutine<Unit> { continuation ->
  continuation.resume(Unit)
 }
 println("After")
}
// Before
// AfterNotice that “After” in the example above is printed because we call
resume in suspendCoroutine. This statement is true, but I need to clarify. You might imagine
that here we suspend and immediately resume. This is a good intuition, but the truth is that there is an optimization that prevents a suspension if resuming is immediate.Speaking of the
optimizationsuspend fun getUsers(): List<User> {
   // no asynchronous work and suspension point here
   // ....
  return users
}suspend fun getUsers(): List<User> {
  suspendCoroutine<Unit> { continuation -> // this code line is definitely a suspention point.
  // list sorting or something but not asynchronous work.
  continuation.resume(Unit)
 }
 return users
}susepndxoangon
09/19/2023, 6:14 AMDontsu
09/19/2023, 6:54 AMPatrick Steiger
09/24/2023, 12:51 AMsuspendCoroutineUninterceptedOrReturnOrReturnCOROUTINE_SUSPENDEDDontsu
10/25/2023, 4:31 AM