title: What is the purpose of coroutine yield()?
body: <p>I'm not exactly sure what is the purpose of the <code>yield</code> function. Can you check this example I have?</p>
  <p>I am following an example <a href="https://kotlinlang.org/docs/reference/coroutines/exception-handling.html#cancellation-and-exceptions" rel="noreferrer">here</a>.</p>
  <p>Here is the code:</p>
  <pre><code>val job = launch {
      val child = launch {
          try {
              delay(Long.MAX_VALUE)
          } finally {
              println(&quot;Child is cancelled&quot;)
          }
      }
      yield() //why do i need this ???????
      println(&quot;Cancelling child&quot;)
      child.cancel()
      child.join()
      yield()
      println(&quot;Parent is not cancelled&quot;)
  }
  job.join()
  </code></pre>
  <p>When I comment out the first yield I get the following results:</p>
  <ul>
  <li><p>Cancelling child</p>
  <p>Parent is not cancelled</p>
  </li>
  </ul>
  <p>but if I leave the yield as it is I get:</p>
  <ul>
  <li><p>Cancelling child</p>
  <p>Child is cancelled</p>
  <p>Parent is not cancelled</p>
  </li>
  </ul>
  <p>What does it mean to use <code>yield</code> here?</p>