Stephan Schroeder
07/22/2019, 3:17 PMfun main() = runBlocking { // this: CoroutineScope
    coroutineScope {
        launch {
            delay(200L)
            println("2Task from runBlocking")
        }
        coroutineScope { // Creates a coroutine scope
            launch {
                delay(500L)
                println("3Task from nested launch")
            }
            delay(100L)
            println("1Task from coroutine scope") // This line will be printed before the nested launch
        }
        println("4Coroutine scope is over") // This line is not printed until the nested launch completes
    }
}withoutclass
07/22/2019, 3:19 PMrunBlockingcoroutineScoperunBlockinggildor
07/22/2019, 3:20 PMgildor
07/22/2019, 3:20 PMgildor
07/22/2019, 3:21 PMStephan Schroeder
07/22/2019, 3:21 PMrunBlockingcoroutineScopewithoutclass
07/22/2019, 3:23 PMcoroutineScoperunBlockingrunBlockingcoroutineScopegildor
07/22/2019, 3:23 PMMarko Mitic
07/22/2019, 3:24 PMcoroutineScopegildor
07/22/2019, 3:25 PMgildor
07/22/2019, 3:28 PMStephan Schroeder
07/22/2019, 3:28 PMcoroutineScopeprintln("4Coroutine scope is over")gildor
07/22/2019, 3:31 PMStephan Schroeder
07/22/2019, 3:31 PMgildor
07/22/2019, 3:32 PMStephan Schroeder
07/22/2019, 3:32 PMgildor
07/22/2019, 3:32 PMgildor
07/22/2019, 3:33 PMStephan Schroeder
07/22/2019, 3:34 PMStephan Schroeder
07/22/2019, 3:34 PMgildor
07/22/2019, 3:34 PMStephan Schroeder
07/22/2019, 3:35 PMsuspendgildor
07/22/2019, 3:35 PMStephan Schroeder
07/22/2019, 3:35 PMgildor
07/22/2019, 3:35 PMStephan Schroeder
07/22/2019, 3:35 PMgildor
07/22/2019, 3:36 PMgildor
07/22/2019, 3:36 PMStephan Schroeder
07/22/2019, 3:36 PMStephan Schroeder
07/22/2019, 3:37 PMStephan Schroeder
07/22/2019, 3:37 PMgildor
07/22/2019, 3:38 PMgildor
07/22/2019, 3:38 PMgildor
07/22/2019, 3:39 PMStephan Schroeder
07/22/2019, 3:39 PMgildor
07/22/2019, 3:39 PMStephan Schroeder
07/22/2019, 3:40 PMgildor
07/22/2019, 3:40 PMStephan Schroeder
07/22/2019, 3:40 PMgildor
07/22/2019, 3:43 PMgildor
07/22/2019, 3:45 PMStephan Schroeder
07/22/2019, 3:51 PMStephan Schroeder
07/23/2019, 12:20 PMgildor
07/23/2019, 12:47 PMStructured Concurrency blog post didn’t helpYou said that “but there are no scopes in the async-await model”, so I just pointed you to description of this approach and why it’s here to avoid confsion
So basically runBlocking is a normal function while coroutineScope is a suspend function but both create a new CoroutineScopeThat’s correct
treats the lambda provided to the function in the same wayYeah, I would say not lambda, but they provide coroutine scope and scope works the same for all builders, not only for those two, but builders with other semantics and usecases (like withContext, launch etc), it provides scope where you can run child coroutines (even background ones) without worry about lifecycle and leaks of them
Stephan Schroeder
07/23/2019, 1:33 PMI would say not lambda,pretty sure it’s a lambda with (CoroutineScope) receiver. Hmm, so i checked the API for coroutineScope
suspend fun <R> coroutineScope(
    block: suspend CoroutineScope.() -> R
): R (source)suspendsuspendgildor
07/23/2019, 3:29 PMgildor
07/23/2019, 3:32 PM