Last active
June 7, 2020 11:30
-
-
Save whoishusni/0102bb77ccdab8ab6e4c5ff5f0a5d854 to your computer and use it in GitHub Desktop.
Little Code Snippet How To Use Coroutine In Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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...") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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