HelloCW
HelloCW

Reputation: 2255

Is the async a keyword of Coroutines in Kotlin?

I'm learning the Coroutines in Kotlin.

The Text A is from the chapters

Code A is from the chapters, it seems that the async is keyword.

The word async make me puzzled, is the async a keyword of Coroutines or not?

Text A

Unlike many other languages with similar capabilities, async and await are not keywords in Kotlin and are not even part of its standard library. Moreover, Kotlin's concept of suspending function provides a safer and less error-prone abstraction for asynchronous operations than futures and promises. kotlinx.coroutines is a rich library for coroutines developed by JetBrains. It contains a number of high-level coroutine-enabled primitives that this guide covers, including launch, async and others.

Code A

val time = measureTimeMillis {
    val one = async { doSomethingUsefulOne() }
    val two = async { doSomethingUsefulTwo() }
    println("The answer is ${one.await() + two.await()}")
}
println("Completed in $time ms")

Upvotes: 1

Views: 140

Answers (1)

curioustechizen
curioustechizen

Reputation: 10672

The only keyword for coroutines in Kotlin is suspend. Everything else is implemented as the functions in the coroutines library.

In your example, async is not a keyword. Rather it is a function. You can confirm this by seeing the import at the top of the file or Ctrl+Click or Cmd+Click on the async

Upvotes: 2

Related Questions