title: How to wait for all coroutines to finish?
body: <p>I'm launching a coroutine and I want it to finish before I resume execution of main thread. </p>
  
  <p>My code simplified looks like this:</p>
  
  <pre><code>fun hello() {
      for (i in 0..100) {
          println("hello")
      }
  }
  
  fun main(args: Array&lt;String&gt;) {
      val job = GlobalScope.launch { hello() } //launch parallel 
      GlobalScope.launch { job.join() }  //try to wait for job to finish
      print("done")
  }
  </code></pre>
  
  <p>The problem is, because <code>job.join()</code> needs to be within a coroutine, the main line of execution is deferred to "done", so the output looks like this:</p>
  
  <pre><code>donehello
  hello
  hello
  hello
  </code></pre>
  
  <p>I want to wait for the job to complete, like  using <code>sync.WaitGroup</code> in Go. So my output would deterministically look like this:</p>
  
  <pre><code>hello
  hello
  hello
  hello
  ...
  done
  </code></pre>
  
  <p>How do I accomplish this?</p>