Skip to content

Instantly share code, notes, and snippets.

@whoishusni
Last active June 7, 2020 11:30
Show Gist options
  • Save whoishusni/0102bb77ccdab8ab6e4c5ff5f0a5d854 to your computer and use it in GitHub Desktop.
Save whoishusni/0102bb77ccdab8ab6e4c5ff5f0a5d854 to your computer and use it in GitHub Desktop.
Little Code Snippet How To Use Coroutine In Kotlin
//Kotlin Coroutine Code Snippet Made By Husni
//Import Dependency
import kotlinx.coroutines.*
fun income(income : Int) : Int {
return income
}
fun payTax(tax : Int) : Int {
return tax
}
fun main() = runBlocking {
launch {
delay(3000)
val incomeValue = async { income(7500) }
val taxValue = async { payTax(3400) }
val profit = incomeValue.await() - taxValue.await()
println("""
My Money Summary :
Income = ${incomeValue.await()}
Tax = ${taxValue.await()}
Profit = $profit
""".trimIndent())
}
delay(1000)
println("Starting Coroutine...")
}
//First Add Kotlin Dependency in Gradle
dependencies {
...
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment