Skip to content

Instantly share code, notes, and snippets.

@anandwana001
Created October 4, 2025 03:08
Show Gist options
  • Select an option

  • Save anandwana001/c597ee3ec2139850d7adda184eac4108 to your computer and use it in GitHub Desktop.

Select an option

Save anandwana001/c597ee3ec2139850d7adda184eac4108 to your computer and use it in GitHub Desktop.
class DataProcessor {
suspend fun processAllData(items: List<String>): List<String> = coroutineScope {
// All child coroutines will complete before returning
items.map { item ->
async(Dispatchers.Default) {
processItem(item)
}
}.awaitAll()
}
private suspend fun processItem(item: String): String {
delay(100)
return item.uppercase()
}
}
fun main() = runBlocking {
val processor = DataProcessor()
val items = listOf("apple", "banana", "cherry")
try {
val results = processor.processAllData(items)
println("Results: $results")
} catch (e: Exception) {
println("Error occurred: ${e.message}")
// All child coroutines are automatically cancelled
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment