title: Kotlin Coroutines - How to block to await/join all jobs?
body: <p>I am new to Kotlin/Coroutines, so hopefully I am just missing something/don't fully understand how to structure my code for the problem I am trying to solve. </p>
  
  <p>Essentially, I am taking a list of strings, and for each item in the list I want to send it to another method to do work (make a network call and return data based on the response). (<em>Edit</em>:) I want all calls to launch concurrently, and block until all calls are done/the response is acted on, and then return a new list with the info of each response.</p>
  
  <p>I probably don't yet fully understand when to use launch/async, but I've tried to following with both launch (with <code>joinAll</code>), and async (with <code>await</code>).</p>
  
  <pre><code>fun processData(lstInputs: List&lt;String&gt;): List&lt;response&gt; {
  
      val lstOfReturnData = mutableListOf&lt;response&gt;()
  
      runBlocking {
          withContext(Dispatchers.IO) {
              val jobs = List(lstInputs.size) {
                  launch {
                      lstOfReturnData.add(networkCallToGetData(lstInputs[it]))
                  }
              }
              jobs.joinAll()
          }
      }
  
      return lstofReturnData
  </code></pre>
  
  <p>What I am expecting to happen, is if my <code>lstInputs</code> is a size of 120, when all jobs are joined, my <code>lstOfReturnData</code> should also have a size of 120.</p>
  
  <p>What actually is happening is inconsitent results. I'll run it once, and I get 118 in my final list, run it again, it's 120, run it again, it's 117, etc. In the <code>networkCallToGetData()</code> method, I am handling any exceptions, to at least return something for every request, regardless if the network call fails. </p>
  
  <p>Can anybody help explain why I am getting inconsistent results, and what I need to do to ensure I am blocking appropriately and all jobs are being joined before moving on?</p>