Last active
October 30, 2019 23:22
-
-
Save pencelab/2d2135edf57b4245fa4eb5f541636d7a to your computer and use it in GitHub Desktop.
Suspend functions to produce a random number.
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
private suspend fun myRandomNumberSuccess(operation: String): Int = withContext(Dispatchers.Default) { | |
log("$operation Retrieving random number...") | |
delay(2000) //Simulating a heavy computation | |
val random = (1..100).random() | |
log("$operation Random Number = $random") | |
random | |
} | |
private suspend fun myRandomNumberFail(operation: String): Int = withContext(Dispatchers.Default) { | |
log("$operation Retrieving random number...") | |
delay(1000) //Simulating a heavy computation | |
throw IllegalStateException("$operation Retrieving random number failed.") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment