title: How to check Kotlin coroutine job.isActive in the calling method
body: <p>I am trying to use Kotlin coroutine instead of legacy Java thread to perform background actions:</p>
  <p>I learned from this <a href="https://medium.com/androiddevelopers/cancellation-in-coroutines-aa6b90163629" rel="nofollow noreferrer">link</a> and it works fine</p>
  <pre><code>val job = launch {
      for(file in files) {
          ensureActive() //will throw cancelled exception to interrupt the execution
          readFile(file)
      }
  }
  </code></pre>
  <p>But my case is that I have a very complex calling function of readFile(), how can I check whether the job is active inside that function?</p>
  <pre><code>val job = launch {
      for(file in files) {
          ensureActive() //will throw cancelled exception to interrupt the execution
          complexFunOfReadingFile(file) //may process each line of the file
      }
  }
  </code></pre>
  <p>I don't want to copy the impl of the function inside this coroutine scope or pass the job instance as a parameter into that function.
  What is official way to handle this case?</p>
  <p>Thanks a lot.</p>