title: Call Kotlin Coroutines in parallel from suspend function in Android
body: <p>I'm relatively new to coroutines, so I was wondering how can I solve my small local problem without much restructuring my Android codes.</p>
  
  <p>Here is a simple setup. My ViewModel calls a <code>suspend</code> function from repository:</p>
  
  <pre><code>// ...ViewModel.kt
  
  fun loadData() {
      viewModelScope.launch {
          val data = dataRepository.loadData()
      }
  }
  </code></pre>
  
  <p>This is quite convenient, since I have a <code>viewModelScope</code> prepared for me by Android and I call a suspend function from my Repository. I don't care how the repository loads the data, I just suspend till it is returned to me.</p>
  
  <p>My data repository makes several calls using <code>Retrofit</code>:</p>
  
  <pre><code>//...DataRepository.kt
  
  @MainThread
  suspend fun loadData(): ... {
      // Retrofit switches the contexts for me, just
      // calling `suspend fun getItems()` here.
      val items = retrofitApi.getItems()
      val itemIDs = items.map { it.id }
  
      // Next, getting overall list of subItems for each item. Again, each call and context
      // switch for `suspend fun retrofitApi.getSubItems(itemID)` is handled by Retrofit.
      val subItems = itemIDs.fold(mutableListOf()) { result, itemID -&gt;
          result.apply {
              addAll(retrofitApi.getSubItems(itemID)) // &lt;- sequential :(
          }
      }
  
      return Pair(items, subItems)
  }
  </code></pre>
  
  <p>As you can see, since <code>loadData()</code> is a suspend function, all the calls to <code>retrofitApi.getSubItem(itemID)</code> will be executed sequentially.</p>
  
  <p>However, I would like to execute them in parallel, something like <code>async() / await()</code> in coroutines would do.</p>
  
  <p>I want to keep the <code>ViewModel</code> codes untouched - it should not care how the data is loaded, just launches a suspend function from own scope. I also don't want to pass any kind of scopes or other objects to my repository.</p>
  
  <p>How can I do this inside a suspend function? Is the scope somehow implicitly present there? Is calling <code>async()</code> possible/allowed/good practice?</p>