Gilles Barbier
09/23/2024, 1:49 PMsuspend fun sendMessage(total: Int) = coroutineScope {
  repeat(total) {
    launch {
      println("sending $it")
      internalSendToAsync(it).await() // internalSendToAsync is a function returning a CompletableFuture
      println("sent $it")
    }
  }
}sendMessage(1000)CoroutineScope(<http://Dispatchers.IO|Dispatchers.IO>).launch {
  sendMessage(1000) 
}.join()internalSendToAsyncsendMessage(1000) 
CoroutineScope(<http://Dispatchers.IO|Dispatchers.IO>).launch {
  sendMessage(1000) 
}.join()Dmitry Khalanskiy [JB]
09/23/2024, 2:09 PMinternalSendToAsync(it)async { delay(100) }internalSendToAsyncsendMessage(1000)runTest { }Gilles Barbier
09/23/2024, 2:20 PMinternalSendToAsyncGilles Barbier
09/23/2024, 2:30 PMinternalSendToAsyncDmitry Khalanskiy [JB]
09/23/2024, 2:31 PMsuspend fun sendMessage(total: Int) = coroutineScope {
  newSingleThreadContext("thread").use { ctx ->
    repeat(total) {
      launch {
        println("sending $it")
        withContext(ctx) {
          internalSendToAsync(it).await()
        }
        println("sent $it")
      }
    }
  }
}internalSendToAsyncGilles Barbier
09/23/2024, 2:56 PM