Suspending functions
Throughout the book, we have written many suspending algorithms, but most of the time it has been done using coroutine builders such as launch()
, async()
, and runBlocking()
. When calling a coroutine builder, the code that we pass to it is a suspending lambda. Now it's time to take a detailed look at how to write suspending code that exists as a function instead:
suspend fun greetDelayed(delayMillis: Int) { delay(delayMillis) println("Hello, World!") }
To create a suspending function, you only need to add the modifier suspend
to its signature. Notice that suspending functions can call other suspending functions such as delay()
directly – no need to wrap inside a coroutine builder– making our code clean and easy to read.
Nevertheless, if we try calling this function outside of a coroutine it will not work. Consider this example:
fun main(args: Array<String>) {
greetDelayed(1000)
}
This code will not compile because, as we already know, suspending computations can be...