Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import kotlinx.coroutines.*
- fun main() {
- runBlocking {
- println("Weather forecast")
- println(getWeatherReport())
- println("Have a good day!")
- }
- }
- suspend fun getWeatherReport() = coroutineScope {
- val forecast = async { getForecast() }
- val temperature = async {
- try {
- getTemperature()
- } catch (e: AssertionError) {
- println("Caught exception $e")
- "{ No temperature found }"
- }
- }
- "${forecast.await()} ${temperature.await()}"
- }
- suspend fun getForecast(): String {
- delay(1000)
- return "Sunny"
- }
- suspend fun getTemperature(): String {
- delay(500)
- throw AssertionError("Temperature is invalid")
- return "30\u00b0C"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement