Coroutine Builders Job
Learn how coroutine builders create their jobs based on their parent's job.
We'll cover the following...
Every coroutine builder from the Kotlin coroutines library creates its own job. Most coroutine builders return their jobs so we can use them elsewhere. This is visible for launch
, where Job
is an explicit result type.
package kotlinx.coroutines.app import kotlinx.coroutines.* fun main(): Unit = runBlocking { val job: Job = launch { delay(1000) println("Test") } }
Job used as an explicit return type
The type returned by the async
function is Deferred<T>
, and Deferred<T>
also implements the Job
interface so we can use it similarly.
Press + to interact
package kotlinx.coroutines.appimport kotlinx.coroutines.*fun main(): Unit = runBlocking {val deferred: Deferred<String> = async {delay(1000)"Test"}val job: Job = deferred}
Since Job
...