title: Getting "Suspension functions can be called only within coroutine body" when calling withContext(Dispatchers.Main) inside a lambda
body: <p>I am creating a coroutine using the script below:</p>
  
  <pre><code>fun bar(completion: () -&gt; Unit) {
    GlobalScope.launch(Dispatchers.IO) {
      val lambda = {
        withContext(Dispatchers.Main) { //Suspension functions can be called only within coroutine body
          completion()
        }
      }
      foo(lambda)
    }
  }
  
  fun foo(lambda: () -&gt; Unit) {
    //...do something heavy
    lambda()
  }
  </code></pre>
  
  <p>But I am getting the error <code>Suspension functions can be called only within coroutine body</code> when I call <code>withContext(Dispatchers.Main)</code> because the lambda changes the context. I cannot change <code>foo(lambda: () -&gt; Unit)</code> to <code>foo(lambda: suspend () -&gt; Unit)</code> because it's from an external library.</p>
  
  <p>Any idea of what I can do to call <code>withContext(Dispatchers.Main)</code> inside the lambda created inside the <code>launch</code> context?</p>
  
  <ul>
  <li>Kotlin version: 1.3.41</li>
  <li>Coroutines: 1.3.0-RC</li>
  </ul>
  
  <p>Thank you!</p>