title: How to propagate the response of an async operation to the view using Jetpack Compose?
body: <p>I have this sealed class:</p>
  <pre><code>sealed class Resource&lt;out T&gt; {
      object Loading: Resource&lt;Nothing&gt;()
      data class Success&lt;out T&gt;(val data: T): Resource&lt;T&gt;()
      data class Failure(val message: String): Resource&lt;Nothing&gt;()
  }
  </code></pre>
  <p>In the repository class I have this function that deletes an item from an API:</p>
  <pre><code>override suspend fun deleteItem(id: String) = flow {
      try {
          emit(Resource.Loading)
          emit(Resource.Success(itemsRef.document(id).delete().await()))
      } catch (e: Exception) {
          emit(Resource.Failure(e.message))
      }
  }
  </code></pre>
  <p>The result of the delete operation is <code>Void?</code>. Now, in the ViewModel class I declare:</p>
  <pre><code>val state = mutableStateOf&lt;Resource&lt;Void?&gt;&gt;(Success(null))
  </code></pre>
  <p>And update it when the delete completes:</p>
  <pre><code>fun deleteItem(id: String) {
      viewModelScope.launch {
          repo.deleteItem(id).collect { response -&gt;
              state.value = response
          }
      }
  }
  </code></pre>
  <p>I have created a Card and inside onClick I have added:</p>
  <pre><code>IconButton(
      onClick = viewModel.deleteItem(id),
  )
  </code></pre>
  <p>Which actually deletes that item form database correctly. But I cannot track the result of the operation. I tried using:</p>
  <pre><code>when(val res = viewModel.state.value) {
      is Resource.Loading -&gt; Log.d(TAG, &quot;Loading&quot;)
      is Resource.Success -&gt; Log.d(TAG, &quot;Success&quot;)
      is Resource.Failure -&gt; Log.d(TAG, &quot;Failure&quot;)
  }
  </code></pre>
  <p>But only the case Loading  is triggered. No success/failure at all. What can be wrong here? As it really acts like a synchronous operation.</p>