Suspend Functions of Kotlin Coroutines

 

Suspending Functions

Suspending functions are a cornerstone of Kotlin Coroutines. They are the functions that can be paused and resumed at a later time. To define a suspending function, you use the suspend modifier. Here's a simple example:


import kotlinx.coroutines.*

suspend fun doSomething() {
delay(1000L)
println("Doing something")
}

fun main() = runBlocking {
launch {
doSomething()
}
}


In this code, doSomething is a suspending function. Inside doSomething, we're delaying for one second and then printing a message. We're calling doSomething from a coroutine, because suspending functions can only be called from another suspending function or a coroutine.

Popular posts from this blog

Simple Sign up design. Android.

Cart page design in android.

Set Date on jDateChooser and retrieve record from database.