1 // This file was automatically generated from composing-suspending-functions.md by Knit tool. Do not edit. 2 package kotlinx.coroutines.guide.exampleCompose04 3 4 import kotlinx.coroutines.* 5 import kotlin.system.* 6 7 // note that we don't have `runBlocking` to the right of `main` in this example mainnull8fun main() { 9 val time = measureTimeMillis { 10 // we can initiate async actions outside of a coroutine 11 val one = somethingUsefulOneAsync() 12 val two = somethingUsefulTwoAsync() 13 // but waiting for a result must involve either suspending or blocking. 14 // here we use `runBlocking { ... }` to block the main thread while waiting for the result 15 runBlocking { 16 println("The answer is ${one.await() + two.await()}") 17 } 18 } 19 println("Completed in $time ms") 20 } 21 22 @OptIn(DelicateCoroutinesApi::class) <lambda>null23fun somethingUsefulOneAsync() = GlobalScope.async { 24 doSomethingUsefulOne() 25 } 26 27 @OptIn(DelicateCoroutinesApi::class) <lambda>null28fun somethingUsefulTwoAsync() = GlobalScope.async { 29 doSomethingUsefulTwo() 30 } 31 doSomethingUsefulOnenull32suspend fun doSomethingUsefulOne(): Int { 33 delay(1000L) // pretend we are doing something useful here 34 return 13 35 } 36 doSomethingUsefulTwonull37suspend fun doSomethingUsefulTwo(): Int { 38 delay(1000L) // pretend we are doing something useful here, too 39 return 29 40 } 41