Sobes.tech
Junior — Middle

What result does a function marked with the async keyword in coroutines return?

sobes.tech AI

Answer from AI

A function declared with the async keyword in Kotlin coroutines returns an object of type Deferred<T>, where T is the type of the result that the function returns.

Deferred is a future result that can be obtained later using the await() function. This allows asynchronous operations to be performed without blocking the main thread.

Example:

import kotlinx.coroutines.*

fun main() = runBlocking {
    val deferred: Deferred<Int> = async {
        delay(1000L) // simulate a long operation
        42
    }
    println("Result: ${deferred.await()}")
}