title: what is the effect using coroutineScope in the runBlocking?
body: <p>in kotlin coroutines <a href="https://github.com/Kotlin/kotlinx.coroutines/blob/master/docs/basics.md" rel="nofollow noreferrer">doc</a>, it explains the &quot;difference between runBlocking and coroutineScope&quot;:</p>
  <blockquote>
  <p>Scope builder</p>
  <p>In addition to the coroutine scope provided by different builders, it is possible to declare your own scope using coroutineScope builder. It creates a coroutine scope and does not complete until all launched children complete. <code>The main difference between runBlocking and coroutineScope is that the latter does not block the current thread while waiting for all children to complete</code>.</p>
  </blockquote>
  <p>I dont quite understand, the code of the sample shows</p>
  <pre><code>import kotlinx.coroutines.*
  
  fun main() = runBlocking { // this: CoroutineScope
  launch { 
      delay(200L)
      println(&quot;+++Task from runBlocking&quot;)
  }
  
  coroutineScope { // Creates a coroutine scope
      launch {
          delay(500L) 
          println(&quot;+++Task from nested launch&quot;)
      }
  
      delay(100L)
      println(&quot;+++ Task from coroutine scope&quot;) // This line will be printed before the nested launch
  }
  
  println(&quot;+++ Coroutine scope is over&quot;) // This line is not printed until the nested launch completes
  }
  </code></pre>
  <p>the log:</p>
  <pre><code>2019-05-16 10:47:45.107 12239-12239 +++ before enter runBlocking{}
  2019-05-16 10:47:45.219 12239-12239 +++ Task from coroutine scope
  2019-05-16 10:47:45.320 12239-12239 +++ Task from runBlocking
  2019-05-16 10:47:45.620 12239-12239 +++ Task from nested launch
  2019-05-16 10:47:45.621 12239-12239 +++ ---after exit runBlocking{}
  </code></pre>
  <p>and the <code>+++ Task from nested launch</code> is show after <code>+++ Task from runBlocking</code> as expected, since it has 500L delay.</p>
  <p>But if adding some some other <code>launch</code> builders after the <code>coroutineScop{}</code> block,  the result is confusing.</p>
  <p>Here two <code>launch</code> are added after the <code>coroutineScop{}</code> with 30L and 100L delay in them.</p>
  <p>I was expecting to see the log for the <code>30L</code> delay should show up before the <code>300L</code>  delay ones inside the <code>coroutineScop{}</code>, but it shows up after all <code>launch</code>  are done inside the <code>coroutineScop{}</code></p>
  <pre><code>fun testCoroutines() {
  Log.e(&quot;+++&quot;, &quot;+++ enter testCoroutines_3&quot;)
  runBlocking {
      launch {
          println(&quot;+++ start Task from runBlocking, with 200L delay&quot;)
          delay(200L)
          println(&quot;+++ end Task from runBlocking, with 200L delay&quot;)
      }
  
      launch {
          println(&quot;+++ start Task from runBlocking, with 50L delay&quot;)
          delay(50L)
          println(&quot;+++ end Task from runBlocking, with 50L delay&quot;)
      }
  
      launch {
          println(&quot;+++ start Task from runBlocking, with 70L delay&quot;)
          delay(70L)
          println(&quot;+++ end Task from runBlocking, with 70L delay&quot;)
      }
  
      coroutineScope {
          println(&quot;+++ enter Task from coroutineScope&quot;)
          // Creates a coroutine scope
          launch {
              Log.v(&quot;+++&quot;, &quot;+++ === start Task from nested launch, 500L&quot;)
              delay(500L)
              Log.v(&quot;+++&quot;, &quot;+++ --- end Task from nested launch, 500L&quot;)
          }
  
          delay(100L)
          println(&quot;+++ in Task from coroutineScope after delay(100L)&quot;)
  
          launch {
              Log.v(&quot;+++&quot;, &quot;+++ === start Task from nested launch, 300L&quot;)
              delay(300L)
              Log.v(&quot;+++&quot;, &quot;+++ --- end Task from nested launch, 300L&quot;)
          }
  
          println(&quot;+++ --- exit Task from coroutine scope&quot;) // This line will be printed before the nested launch
      }
  
      launch {
          println(&quot;+++ start Task from runBlocking, with 30L delay&quot;)
          delay(30L)
          println(&quot;+++ end Task from runBlocking, with 30L delay&quot;)
      }
  
      launch {
          println(&quot;+++ start Task from runBlocking, with 100L delay&quot;)
          delay(100L)
          println(&quot;+++ end Task from runBlocking, with 100L delay&quot;)
      }
  
  }
  
  Log.e(&quot;+++&quot;, &quot;--- exit  testCoroutines_3 scope is over&quot;) // This line is not printed until the nested launch completes
  
  }
  </code></pre>
  <p>the log:</p>
  <pre><code>10:35:05.819 4657-4657 +++ enter testCoroutines_3
  10:35:05.828 4657-4657 +++ enter Task from coroutineScope
  10:35:05.833 4657-4657 +++ start Task from runBlocking, with 200L delay
  10:35:05.833 4657-4657 +++ start Task from runBlocking, with 50L delay
  10:35:05.833 4657-4657 +++ start Task from runBlocking, with 70L delay
  10:35:05.834 4657-4657 +++ === start Task from nested launch, 500L
  10:35:05.885 4657-4657 +++ end Task from runBlocking, with 50L delay
  10:35:05.905 4657-4657 +++ end Task from runBlocking, with 70L delay
  10:35:05.932 4657-4657 +++ in Task from coroutineScope after delay(100L)
  10:35:05.933 4657-4657 +++ --- exit Task from coroutine scope
  10:35:05.935 4657-4657 +++ === start Task from nested launch, 300L
  10:35:06.034 4657-4657 +++ end Task from runBlocking, with 200L delay
  10:35:06.235 4657-4657 +++ --- end Task from nested launch, 300L
  10:35:06.334 4657-4657 +++ --- end Task from nested launch, 500L
  10:35:06.335 4657-4657 +++ start Task from runBlocking, with 30L delay
  10:35:06.335 4657-4657 +++ start Task from runBlocking, with 100L delay
  10:35:06.366 4657-4657 +++ end Task from runBlocking, with 30L delay
  10:35:06.436 4657-4657 +++ end Task from runBlocking, with 100L delay
  10:35:06.437 4657-4657--- exit  testCoroutines_3 scope is over
  </code></pre>
  <p>thought at least the <code>+++ start Task from runBlocking, with 30L delay</code>  should show up earlier at after the <code>+++ === start Task from nested launch, 500L</code> and before <code>+++ end Task from runBlocking, with 50L delay</code>,  but it doesnt and shows up after all <code>launch</code> are done at <code>+++ --- end Task from nested launch, 500L</code>.</p>
  <p>What does the <code>coroutineScope</code> do in the coroutines block?</p>
  <p>(I was testing using android app with a button click to call the <code>testCoroutines</code>)</p>